Project

General

Profile

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

    
69
	class updatedns {
70
		var $_cacheFile;
71
		var $_debugFile;
72
		var $_UserAgent = 'User-Agent: phpDynDNS/0.7';
73
		var $_errorVerbosity = 0;
74
		var $_dnsService;
75
		var $_dnsUser;
76
		var $_dnsPass;
77
		var $_dnsHost;
78
		var $_dnsIP;
79
		/* This is needed for support on addresses behind NAT. */
80
		var $_ifIP;
81
		var $_dnsWildcard;
82
		var $_dnsMX;
83
		var $_dnsBackMX;
84
		var $_dnsServer;
85
		var $_dnsPort;
86
		var $_dnsUpdateURL;
87
		var $status;
88
		var $_debugID;
89
		var $_if;
90
		
91
		/* 
92
		 * Public Constructor Function (added 12 July 05) [beta]
93
		 *   - Gets the dice rolling for the update. 
94
		 */
95
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '',
96
				    $dnsWildcard = 'OFF', $dnsMX = '', $dnsIf = '', $dnsBackMX = '',
97
				    $dnsServer = '', $dnsPort = '', $dnsUpdateURL = '', $forceUpdate = false) {
98
			
99
			global $config, $g;
100
			
101
			$this->_cacheFile = "{$g['conf_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . ".cache";
102
			$this->_debugFile = "{$g['varetc_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . ".debug";
103

    
104
			log_error("DynDns: updatedns() starting");
105

    
106
			$dyndnslck = lock($dnsHost, LOCK_EX);
107

    
108
			if (!$dnsService) $this->_error(2);
109
			switch ($dnsService) {
110
			case 'freedns':
111
				if (!$dnsHost) $this->_error(5);
112
				break;
113
			case 'namecheap':
114
				if (!$dnsPass) $this->_error(4);
115
				if (!$dnsHost) $this->_error(5);
116
				break;
117
			default:
118
				if (!$dnsUser) $this->_error(3);
119
				if (!$dnsPass) $this->_error(4);
120
				if (!$dnsHost) $this->_error(5);
121
			}
122
			
123
			$this->_dnsService = strtolower($dnsService);
124
			$this->_dnsUser = $dnsUser;
125
			$this->_dnsPass = $dnsPass;
126
			$this->_dnsHost = $dnsHost;
127
			$this->_dnsServer = $dnsServer;
128
			$this->_dnsPort = $dnsPort;
129
			$this->_dnsWildcard = $dnsWildcard;
130
			$this->_dnsMX = $dnsMX;
131
			$this->_if = get_real_interface($dnsIf);
132
			$this->_ifIP = get_interface_ip($dnsIf);
133

    
134
			// Ensure that we where able to lookup the IP
135
			if(!is_ipaddr($this->_ifIP)) {
136
				log_error("There was an error trying to determine the IP for interface - {$dnsIf}({$this->_if}). Probably interface has no ip or is down. Dyndns update not possible for {$dnsService}.");
137
				unlock($dyndnslck);
138
				return;
139
			}
140

    
141
			$this->_debugID = rand(1000000, 9999999);
142
			
143
			if ($forceUpdate == false && $this->_detectChange() == false) {
144
				$this->_error(10);
145
			} else {
146
				switch ($this->_dnsService) {
147
				case 'dnsomatic':
148
				case 'dyndns':
149
				case 'dyndns-static':
150
				case 'dyndns-custom':
151
				case 'dhs':
152
				case 'noip':
153
				case 'easydns':
154
				case 'hn':
155
				case 'zoneedit':
156
				case 'dyns':
157
				case 'ods':
158
				case 'freedns':
159
				case 'loopia':
160
				case 'staticcling':
161
				case 'dnsexit':
162
				case 'opendns':
163
				case 'namecheap':
164
				case 'he-net':
165
				case 'selfhost':
166
					$this->_update();	
167
					break;
168
				case 'he-net-tunnelbroker':
169
					$this->_update();
170
					break;
171
				default:
172
					$this->_error(6);
173
					break;
174
				}
175
			}
176

    
177
			unlock($dyndnslck);
178
		}
179
			
180
		/*
181
		 * Private Function (added 12 July 05) [beta]
182
		 *   Send Update To Selected Service.
183
		 */
184
		function _update() {
185
		
186
			log_error("DynDns: DynDns _update() starting.");
187
		
188
			if ($this->_dnsService != 'ods') {
189
				$ch = curl_init();
190
				curl_setopt($ch, CURLOPT_HEADER, 0);
191
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
192
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
193
				curl_setopt($ch, CURLOPT_INTERFACE, $this->_ifIP);
194
				curl_setopt($ch, CURLOPT_TIMEOUT, 120); // Completely empirical
195
			}
196

    
197
			switch ($this->_dnsService) {
198
				case 'dyndns':
199
				case 'dyndns-static':
200
				case 'dyndns-custom':
201
					$needsIP = FALSE;
202
					//log_error("DynDns: DynDns _update() starting. Dynamic");
203
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
204
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
205
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
206
					$server = "https://members.dyndns.org/nic/update";
207
					$port = "";
208
					if($this->_dnsServer)
209
						$server = $this->_dnsServer;
210
					if($this->_dnsPort)
211
						$port = ":" . $this->_dnsPort;
212
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
213
					break;
214
				case 'dhs':
215
					$needsIP = TRUE;
216
					$post_data['hostscmd'] = 'edit';
217
					$post_data['hostscmdstage'] = '2';
218
					$post_data['type'] = '4';
219
					$post_data['updatetype'] = 'Online';
220
					$post_data['mx'] = $this->_dnsMX;
221
					$post_data['mx2'] = '';
222
					$post_data['txt'] = '';
223
					$post_data['offline_url'] = '';
224
					$post_data['cloak'] = 'Y';
225
					$post_data['cloak_title'] = '';
226
					$post_data['ip'] = $this->_dnsIP;
227
					$post_data['domain'] = 'dyn.dhs.org';
228
					$post_data['hostname'] = $this->_dnsHost;
229
					$post_data['submit'] = 'Update';
230
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
231
					$server = "https://members.dhs.org/nic/hosts";
232
					$port = "";
233
					if($this->_dnsServer)
234
						$server = $this->_dnsServer;
235
					if($this->_dnsPort)
236
						$port = ":" . $this->_dnsPort;					
237
					curl_setopt($ch, CURLOPT_URL, '{$server}{$port}');
238
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
239
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
240
					break;
241
				case 'noip':
242
					$needsIP = TRUE;
243
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
244
					$server = "http://dynupdate.no-ip.com/ducupdate.php";
245
					$port = "";
246
					if($this->_dnsServer)
247
						$server = $this->_dnsServer;
248
					if($this->_dnsPort)
249
						$port = ":" . $this->_dnsPort;
250
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&pass=' . urlencode($this->_dnsPass) . '&hostname=' . $this->_dnsHost.'&ip=' . $this->_dnsIP);
251
					break;
252
				case 'easydns':
253
					$needsIP = TRUE;
254
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
255
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
256
					$server = "http://members.easydns.com/dyn/dyndns.php";
257
					$port = "";
258
					if($this->_dnsServer)
259
						$server = $this->_dnsServer;
260
					if($this->_dnsPort)
261
						$port = ":" . $this->_dnsPort;
262
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard=' . $this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=' . $this->_dnsBackMX);
263
					break;
264
				case 'hn':
265
					$needsIP = TRUE;
266
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
267
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
268
					$server = "http://dup.hn.org/vanity/update";
269
					$port = "";
270
					if($this->_dnsServer)
271
						$server = $this->_dnsServer;
272
					if($this->_dnsPort)
273
						$port = ":" . $this->_dnsPort;
274
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?ver=1&IP=' . $this->_dnsIP);
275
					break;
276
				case 'zoneedit':
277
					$needsIP = FALSE;
278
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
279
					curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
280
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
281

    
282
					$server = "https://dynamic.zoneedit.com/auth/dynamic.html";
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}?host=" .$this->_dnsHost);
289
					break;
290
				case 'dyns':
291
					$needsIP = FALSE;
292
					$server = "http://www.dyns.cx/postscript011.php";
293
					$port = "";
294
					if($this->_dnsServer)
295
						$server = $this->_dnsServer;
296
					if($this->_dnsPort)
297
						$port = ":" . $this->_dnsPort;					
298
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&password=' . $this->_dnsPass . '&host=' . $this->_dnsHost);
299
					break;
300
				case 'ods':
301
					$needsIP = FALSE;
302
					$misc_errno = 0;
303
					$misc_error = "";
304
					$server = "ods.org";
305
					$port = "";
306
					if($this->_dnsServer)
307
						$server = $this->_dnsServer;
308
					if($this->_dnsPort)
309
						$port = ":" . $this->_dnsPort;						
310
					$this->con['socket'] = fsockopen("{$server}{$port}", "7070", $misc_errno, $misc_error, 30);
311
					/* Check that we have connected */
312
					if (!$this->con['socket']) {
313
						print "error! could not connect.";
314
						break;
315
					}
316
					/* Here is the loop. Read the incoming data (from the socket connection) */
317
					while (!feof($this->con['socket'])) {
318
						$this->con['buffer']['all'] = trim(fgets($this->con['socket'], 4096));
319
						$code = substr($this->con['buffer']['all'], 0, 3);
320
						sleep(1);
321
						switch($code) {
322
							case 100:
323
								fputs($this->con['socket'], "LOGIN ".$this->_dnsUser." ".$this->_dnsPass."\n");
324
								break;
325
							case 225:
326
								fputs($this->con['socket'], "DELRR ".$this->_dnsHost." A\n");
327
								break;
328
							case 901:
329
								fputs($this->con['socket'], "ADDRR ".$this->_dnsHost." A ".$this->_dnsIP."\n");
330
								break;
331
							case 795:
332
								fputs($this->con['socket'], "QUIT\n");
333
								break;
334
						}
335
					}
336
					$this->_checkStatus(0, $code);
337
					break;
338
				case 'freedns':
339
					$needIP = FALSE;
340
					curl_setopt($ch, CURLOPT_URL, 'http://freedns.afraid.org/dynamic/update.php?' . $this->_dnsPass);
341
					break;
342
				case 'dnsexit':
343
					$needsIP = TRUE;
344
					curl_setopt($ch, CURLOPT_URL, 'http://www.dnsexit.com/RemoteUpdate.sv?login='.$this->_dnsUser. '&password='.$this->_dnsPass.'&host='.$this->_dnsHost.'&myip='.$this->_dnsIP);
345
					break;
346
				case 'loopia':
347
					$needsIP = TRUE;
348
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
349
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
350
					curl_setopt($ch, CURLOPT_URL, 'https://dns.loopia.se/XDynDNSServer/XDynDNS.php?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP);
351
					break;
352
				case 'opendns':
353
					$needsIP = FALSE;
354
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
355
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
356
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
357
					$server = "https://updates.opendns.com/nic/update?hostname=". $this->_dnsHost;
358
					$port = "";
359
					if($this->_dnsServer)
360
						$server = $this->_dnsServer;
361
					if($this->_dnsPort)
362
						$port = ":" . $this->_dnsPort;
363
					curl_setopt($ch, CURLOPT_URL, $server .$port);
364
					break;
365

    
366
				case 'staticcling':
367
					$needsIP = FALSE;
368
					curl_setopt($ch, CURLOPT_URL, 'http://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
369
					break;	                    
370
				case 'dnsomatic':
371
					/* Example syntax 
372
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
373
					*/
374
					$needsIP = FALSE;
375
					log_error("DNS-O-Matic: DNS update() starting.");
376
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
377
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
378
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
379
					$server = "https://" . $this->_dnsUser . ":" . $this->_dnsPass . "@updates.dnsomatic.com/nic/update?hostname=";
380
					if($this->_dnsServer)
381
						$server = $this->_dnsServer;
382
					if($this->_dnsPort)
383
						$port = ":" . $this->_dnsPort;
384
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
385
					break;
386
				case 'namecheap':
387
					/* Example:
388
						https://dynamicdns.park-your-domain.com/update?host=[host_name]&domain=[domain.com]&password=[domain_password]&ip=[your_ip]
389
					*/
390
					$needsIP = FALSE;
391
					log_error("Namecheap: DNS update() starting.");
392
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
393
					$dparts = explode(".", $this->_dnsHost);
394
					$domain_part_count = ($dparts[count($dparts)-1] == "uk") ? 3 : 2;
395
					$domain_offset = count($dparts) - $domain_part_count;
396
					$hostname = implode(".", array_slice($dparts, 0, $domain_offset));
397
					$domain = implode(".", array_slice($dparts, $domain_offset));
398
					$server = "https://dynamicdns.park-your-domain.com/update?host={$hostname}&domain={$domain}&password={$this->_dnsPass}&ip={$this->_dnsIP}";
399
					curl_setopt($ch, CURLOPT_URL, $server);
400
					break;
401
				case 'he-net':
402
					$needsIP = FALSE;
403
					log_error("HE.net: DNS update() starting.");
404
					$server = "https://dyn.dns.he.net/nic/update?";
405
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
406
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsHost . ':' . $this->_dnsPass);
407
					curl_setopt($ch, CURLOPT_URL, $server . 'hostname=' . $this->_dnsHost);
408
					break;
409
				case 'he-net-tunnelbroker':
410
					$needsIP = FALSE;
411
					log_error("HE.net Tunnelbroker: DNS update() starting.");
412
					$server = "https://ipv4.tunnelbroker.net/ipv4_end.php?";
413
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
414
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser . ':' . $this->_dnsPass);
415
					curl_setopt($ch, CURLOPT_URL, $server . 'tid=' . $this->_dnsHost);
416
					break;
417
				case 'selfhost':
418
					$needsIP = FALSE;
419
					//log_error("SelfHost: DNS update() starting.");
420
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
421
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
422
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
423
					$server = "https://carol.selfhost.de/nic/update";
424
					$port = "";
425
					if($this->_dnsServer)
426
						$server = $this->_dnsServer;
427
					if($this->_dnsPort)
428
						$port = ":" . $this->_dnsPort;
429
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
430
					break;
431
				default:
432
					break;
433
			}
434
			if ($this->_dnsService != 'ods') {
435
				$data = curl_exec($ch);
436
				$this->_checkStatus($ch, $data);
437
				@curl_close($ch);
438
			}
439
		}
440

    
441
		/*
442
		 * Private Function (added 12 July 2005) [beta]
443
		 *   Retrieve Update Status
444
		 */
445
		function _checkStatus($ch, $data) {
446
			log_error("DynDns: DynDns _checkStatus() starting.");
447
			log_error("DynDns: Current Service: {$this->_dnsService}");
448
			$successful_update = false;
449
			if ($this->_dnsService != 'ods' && @curl_error($ch)) {
450
				$status = "Curl error occurred: " . curl_error($ch);
451
				log_error($status);
452
				$this->status = $status;
453
				return;
454
			}
455
			switch ($this->_dnsService) {
456
				case 'dnsomatic':
457
					if (preg_match('/badauth/i', $data)) {
458
						$status = "DNS-O-Matic: The DNS-O-Matic username or password specified are incorrect. No updates will be distributed to services until this is resolved.";
459
					} else if (preg_match('/notfqdn /i', $data)) {
460
						$status = "DNS-O-Matic: The hostname specified is not a fully-qualified domain name. If no hostnames included, notfqdn will be returned once.";
461
					} else if (preg_match('/nohost/i', $data)) {
462
						$status = "DNS-O-Matic: The hostname passed could not be matched to any services configured. The service field will be blank in the return code.";
463
					} else if (preg_match('/numhost/i', $data)) {
464
						$status = "DNS-O-Matic: You may update up to 20 hosts. numhost is returned if you try to update more than 20 or update a round-robin.";	
465
					} else if (preg_match('/abuse/i', $data)) {
466
						$status = "DNS-O-Matic: The hostname is blocked for update abuse.";
467
					} else if (preg_match('/good/i', $data)) {
468
						$status = "DNS-O-Matic: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
469
						$successful_update = true;
470
					} else if (preg_match('/dnserr/i', $data)) {
471
						$status = "DNS-O-Matic: DNS error encountered. Stop updating for 30 minutes.";
472
					} else {
473
						$status = "DNS-O-Matic: (Unknown Response)";
474
						log_error("DNS-O-Matic: PAYLOAD: {$data}");
475
						$this->_debug($data);
476
					}
477
					break;
478
				case 'dyndns':
479
					if (preg_match('/notfqdn/i', $data)) {
480
						$status = "phpDynDNS: (Error) Not A FQDN!";
481
					} else if (preg_match('/nochg/i', $data)) {
482
						$status = "phpDynDNS: (Success) No Change In IP Address";
483
						$successful_update = true;
484
					} else if (preg_match('/good/i', $data)) {
485
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
486
						$successful_update = true;
487
					} else if (preg_match('/noauth/i', $data)) {
488
						$status = "phpDynDNS: (Error) User Authorization Failed";
489
					} else {
490
						$status = "phpDynDNS: (Unknown Response)";
491
						log_error("phpDynDNS: PAYLOAD: {$data}");
492
						$this->_debug($data);
493
					}
494
					break;
495
				case 'dyndns-static':
496
					if (preg_match('/notfqdn/i', $data)) {
497
						$status = "phpDynDNS: (Error) Not A FQDN!";
498
					} else if (preg_match('/nochg/i', $data)) {
499
						$status = "phpDynDNS: (Success) No Change In IP Address";
500
						$successful_update = true;
501
					} else if (preg_match('/good/i', $data)) {
502
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
503
						$successful_update = true;
504
					} else if (preg_match('/noauth/i', $data)) {
505
						$status = "phpDynDNS: (Error) User Authorization Failed";
506
					} else {
507
						$status = "phpDynDNS: (Unknown Response)";
508
						log_error("phpDynDNS: PAYLOAD: {$data}");
509
						$this->_debug($data);
510
					}
511
					break;
512
				case 'dyndns-custom':
513
					if (preg_match('/notfqdn/i', $data)) {
514
						$status = "phpDynDNS: (Error) Not A FQDN!";
515
					} else if (preg_match('/nochg/i', $data)) {
516
						$status = "phpDynDNS: (Success) No Change In IP Address";
517
						$successful_update = true;
518
					} else if (preg_match('/good/i', $data)) {
519
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
520
						$successful_update = true;
521
					} else if (preg_match('/noauth/i', $data)) {
522
						$status = "phpDynDNS: (Error) User Authorization Failed";
523
					} else {
524
						$status = "phpDynDNS: (Unknown Response)";
525
						log_error("phpDynDNS: PAYLOAD: {$data}");
526
						$this->_debug($data);
527
					}
528
					break;
529
				case 'dhs':
530
					break;
531
				case 'noip':
532
					list($ip,$code) = explode(":",$data);
533
					switch ($code) {
534
						case 0:
535
							$status = "phpDynDNS: (Success) IP address is current, no update performed.";
536
							$successful_update = true;
537
							break;
538
						case 1:
539
							$status = "phpDynDNS: (Success) DNS hostname update successful.";
540
							$successful_update = true;
541
							break;
542
						case 2:
543
							$status = "phpDynDNS: (Error) Hostname supplied does not exist.";
544
							break;
545
						case 3:
546
							$status = "phpDynDNS: (Error) Invalid Username.";
547
							break;
548
						case 4:
549
							$status = "phpDynDNS: (Error) Invalid Password.";
550
							break;
551
						case 5:
552
							$status = "phpDynDNS: (Error) To many updates sent.";
553
							break;
554
						case 6:
555
							$status = "phpDynDNS: (Error) Account disabled due to violation of No-IP terms of service.";
556
							break;
557
						case 7:
558
							$status = "phpDynDNS: (Error) Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.";
559
							break;
560
						case 8:
561
							$status = "phpDynDNS: (Error) Disabled / Locked Hostname.";
562
							break;
563
						case 9:
564
							$status = "phpDynDNS: (Error) Host updated is configured as a web redirect and no update was performed.";
565
							break;
566
						case 10:
567
							$status = "phpDynDNS: (Error) Group supplied does not exist.";
568
							break;
569
						case 11:
570
							$status = "phpDynDNS: (Success) DNS group update is successful.";
571
							$successful_update = true;
572
							break;
573
						case 12:
574
							$status = "phpDynDNS: (Success) DNS group is current, no update performed.";
575
							$successful_update = true;
576
							break;
577
						case 13:
578
							$status = "phpDynDNS: (Error) Update client support not available for supplied hostname or group.";
579
							break;
580
						case 14:
581
							$status = "phpDynDNS: (Error) Hostname supplied does not have offline settings configured.";
582
							break;
583
						case 99:
584
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
585
							break;
586
						case 100:
587
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
588
							break;
589
						default:
590
							$status = "phpDynDNS: (Unknown Response)";
591
							$this->_debug("Unknown Response: ".$data);
592
							break;
593
					}
594
					break;
595
				case 'easydns':
596
					if (preg_match('/NOACCESS/i', $data)) {
597
						$status = "phpDynDNS: (Error) Authentication Failed: Username and/or Password was Incorrect.";
598
					} else if (preg_match('/NOSERVICE/i', $data)) {
599
						$status = "phpDynDNS: (Error) No Service: Dynamic DNS Service has been disabled for this domain.";
600
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
601
						$status = "phpDynDNS: (Error) Illegal Input: Self-Explantory";
602
					} else if (preg_match('/TOOSOON/i', $data)) {
603
						$status = "phpDynDNS: (Error) Too Soon: Not Enough Time Has Elapsed Since Last Update";
604
					} else if (preg_match('/NOERROR/i', $data)) {
605
						$status = "phpDynDNS: (Success) IP Updated Successfully!";
606
						$successful_update = true;
607
					} else {
608
						$status = "phpDynDNS: (Unknown Response)";
609
						log_error("phpDynDNS: PAYLOAD: {$data}");
610
						$this->_debug($data);
611
					}
612
					break;
613
				case 'hn':
614
					/* FIXME: add checks */
615
					break;
616
				case 'zoneedit':
617
					if (preg_match('/799/i', $data)) {
618
						$status = "phpDynDNS: (Error 799) Update Failed!";				
619
					} else if (preg_match('/700/i', $data)) {
620
						$status = "phpDynDNS: (Error 700) Update Failed!";
621
					} else if (preg_match('/200/i', $data)) {
622
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
623
						$successful_update = true;
624
					} else if (preg_match('/201/i', $data)) {
625
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
626
						$successful_update = true;						
627
					} else {
628
						$status = "phpDynDNS: (Unknown Response)";
629
						log_error("phpDynDNS: PAYLOAD: {$data}");
630
						$this->_debug($data);
631
					}
632
					break;
633
				case 'dyns':
634
					if (preg_match("/400/i", $data)) {
635
						$status = "phpDynDNS: (Error) Bad Request - The URL was malformed. Required parameters were not provided.";
636
					} else if (preg_match('/402/i', $data)) {
637
						$status = "phpDynDNS: (Error) Update Too Soon - You have tried updating to quickly since last change.";
638
					} else if (preg_match('/403/i', $data)) {
639
						$status = "phpDynDNS: (Error) Database Error - There was a server-sided database error.";
640
					} else if (preg_match('/405/i', $data)) {
641
						$status = "phpDynDNS: (Error) Hostname Error - The hostname (".$this->_dnsHost.") doesn't belong to you.";
642
					} else if (preg_match('/200/i', $data)) {
643
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
644
						$successful_update = true;
645
					} else {
646
						$status = "phpDynDNS: (Unknown Response)";
647
						log_error("phpDynDNS: PAYLOAD: {$data}");
648
						$this->_debug($data);
649
					}
650
					break;
651
				case 'ods':
652
					if (preg_match("/299/i", $data)) {
653
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
654
						$successful_update = true;
655
					} else {
656
						$status = "phpDynDNS: (Unknown Response)";
657
						log_error("phpDynDNS: PAYLOAD: {$data}");
658
						$this->_debug($data);
659
					}
660
					break;
661
				case 'freedns':
662
					if (preg_match("/has not changed./i", $data)) {
663
						$status = "phpDynDNS: (Success) No Change In IP Address";
664
						$successful_update = true;
665
					} else if (preg_match("/Updated/i", $data)) {
666
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
667
						$successful_update = true;
668
					} else {
669
						$status = "phpDynDNS: (Unknown Response)";
670
						log_error("phpDynDNS: PAYLOAD: {$data}");
671
						$this->_debug($data);
672
					} 
673
					break;
674
				case 'dnsexit':
675
					if (preg_match("/is the same/i", $data)) {
676
						$status = "phpDynDns: (Success) No Change In IP Address";
677
						$successful_update = true;
678
					} else if (preg_match("/Success/i", $data)) {
679
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
680
						$successful_update = true;
681
					} else {
682
						$status = "phpDynDNS: (Unknown Response)";
683
                                                log_error("phpDynDNS: PAYLOAD: {$data}");
684
                                                $this->_debug($data);
685
					}
686
					break;
687
				case 'loopia':
688
					if (preg_match("/nochg/i", $data)) {
689
						$status = "phpDynDNS: (Success) No Change In IP Address";
690
						$successful_update = true;
691
					} else if (preg_match("/good/i", $data)) {
692
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
693
						$successful_update = true;
694
					} else if (preg_match('/badauth/i', $data)) {
695
						$status = "phpDynDNS: (Error) User Authorization Failed";
696
					} else {
697
						$status = "phpDynDNS: (Unknown Response)";
698
						log_error("phpDynDNS: PAYLOAD: {$data}");
699
						$this->_debug($data);
700
					}
701
					break;
702
				case 'opendns':
703
					if (preg_match('/badauth/i', $data)) {
704
						$status = "phpDynDNS: (Error) Not a valid username or password!";
705
					} else if (preg_match('/nohost/i', $data)) {
706
						$status = "phpDynDNS: (Error) Hostname you are trying to update does not exist.";
707
						$successful_update = true;
708
					} else if (preg_match('/good/i', $data)) {
709
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
710
						$successful_update = true;
711
					} else if (preg_match('/yours/i', $data)) {
712
						$status = "phpDynDNS: (Error) hostname specified exists, but not under the username specified.";
713
					} else if (preg_match('/abuse/i', $data)) {
714
						$status = "phpDynDns: (Error) Updating to frequently, considered abuse.";
715
					} else {
716
						$status = "phpDynDNS: (Unknown Response)";
717
						log_error("phpDynDNS: PAYLOAD: {$data}");
718
						$this->_debug($data);
719
					}
720
					break;
721
                 case 'staticcling':
722
					if (preg_match("/invalid ip/i", $data)) {
723
					        $status = "phpDynDNS: (Error) Bad Request - The IP provided was invalid.";
724
					} else if (preg_match('/required info missing/i', $data)) {
725
					        $status = "phpDynDNS: (Error) Bad Request - Required parameters were not provided.";
726
					} else if (preg_match('/invalid characters/i', $data)) {
727
					        $status = "phpDynDNS: (Error) Bad Request - Illegal characters in either the username or the password.";
728
					} else if (preg_match('/bad password/i', $data)) {
729
					        $status = "phpDynDNS: (Error) Invalid password.";
730
					} else if (preg_match('/account locked/i', $data)) {
731
					        $status = "phpDynDNS: (Error) This account has been administratively locked.";
732
					} else if (preg_match('/update too frequent/i', $data)) {
733
					        $status = "phpDynDNS: (Error) Updating too frequently.";
734
					} else if (preg_match('/DB error/i', $data)) {
735
					        $status = "phpDynDNS: (Error) Server side error.";
736
					} else if (preg_match('/success/i', $data)) {
737
					        $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
738
					        $successful_update = true;
739
					} else {
740
					        $status = "phpDynDNS: (Unknown Response)";
741
					        log_error("phpDynDNS: PAYLOAD: {$data}");
742
					        $this->_debug($data);
743
					}
744
					break;
745
				case 'namecheap':
746
					$tmp = str_replace("^M", "", $data);
747
					$ncresponse = @xml2array($tmp);
748
					if (preg_match("/internal server error/i", $data)) {
749
						$status = "phpDynDNS: (Error) Server side error.";
750
					} else if ($ncresponse['interface-response']['ErrCount'] === "0") {
751
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
752
						$successful_update = true;
753
					} else if (is_numeric($ncresponse['interface-response']['ErrCount']) && ($ncresponse['interface-response']['ErrCount'] > 0)) {
754
						$status = "phpDynDNS: (Error) " . implode(", ", $ncresponse["interface-response"]["errors"]);
755
						$successful_update = true;
756
					} else {
757
						$status = "phpDynDNS: (Unknown Response)";
758
						log_error("phpDynDNS: PAYLOAD: {$data}");
759
						$this->_debug($data);
760
					}
761
					break;
762
					
763
				case 'he-net':
764
					if (preg_match("/badip/i", $data)) {
765
					        $status = "phpDynDNS: (Error) Bad Request - The IP provided was invalid.";
766
					} else if (preg_match('/nohost/i', $data)) {
767
					        $status = "phpDynDNS: (Error) Bad Request - A hostname was not provided.";
768
					} else if (preg_match('/badauth/i', $data)) {
769
					        $status = "phpDynDNS: (Error) Invalid username or password.";
770
					} else if (preg_match('/good/i', $data)) {
771
					        $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
772
					        $successful_update = true;
773
					} else if (preg_match('/nochg/i', $data)) {
774
							$status = "phpDynDNS: (Success) No Change In IP Address.";
775
							$successful_update = true;
776
					} else {
777
					        $status = "phpDynDNS: (Unknown Response)";
778
					        log_error("phpDynDNS: PAYLOAD: {$data}");
779
					        $this->_debug($data);
780
					}
781
					break;
782
				case 'he-net-tunnelbroker':
783
					/*
784
					-ERROR: Missing parameter(s).
785
					-ERROR: Invalid API key or password
786
					-ERROR: Tunnel not found
787
					-ERROR: Another tunnel exists for this IP.
788
					-ERROR: This tunnel is already associated with this IP address
789
					+OK: Tunnel endpoint updated to: x.x.x.x
790
					*/
791
					if (preg_match("/Missing parameter/i", $data)) {
792
					        $status = "phpDynDNS: (Error) Bad Request - Missing/Invalid Parameters.";
793
					} else if (preg_match('/Tunnel not found/i', $data)) {
794
					        $status = "phpDynDNS: (Error) Bad Request - Invalid Tunnel ID.";
795
					} else if (preg_match('/Invalid API key or password/i', $data)) {
796
					        $status = "phpDynDNS: (Error) Invalid username or password.";
797
					} else if (preg_match('/OK:/i', $data)) {
798
					        $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
799
					        $successful_update = true;
800
					} else if (preg_match('/This tunnel is already associated with this IP address/i', $data)) {
801
							$status = "phpDynDNS: (Success) No Change In IP Address.";
802
							$successful_update = true;
803
					} else {
804
					        $status = "phpDynDNS: (Unknown Response)";
805
					        log_error("phpDynDNS: PAYLOAD: {$data}");
806
					        $this->_debug($data);
807
					}
808
					break;
809
				case 'selfhost':
810
					if (preg_match('/notfqdn/i', $data)) {
811
						$status = "phpDynDNS: (Error) Not A FQDN!";
812
					} else if (preg_match('/nochg/i', $data)) {
813
						$status = "phpDynDNS: (Success) No Change In IP Address";
814
						$successful_update = true;
815
					} else if (preg_match('/good/i', $data)) {
816
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
817
						$successful_update = true;
818
					} else if (preg_match('/noauth/i', $data)) {
819
						$status = "phpDynDNS: (Error) User Authorization Failed";
820
					} else {
821
						$status = "phpDynDNS: (Unknown Response)";
822
						log_error("phpDynDNS: PAYLOAD: {$data}");
823
						$this->_debug($data);
824
					}
825
					break;
826
			}
827
			
828
			if($successful_update == true) {
829
				/* Write WAN IP to cache file */
830
				$wan_ip = $this->_checkIP();
831
				conf_mount_rw();
832
				if ($wan_ip > 0) {
833
					$currentTime = time();				  
834
					log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
835
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
836
				} else
837
					@unlink($this->_cacheFile);
838
				conf_mount_ro();
839
			}
840
			$this->status = $status;
841
			log_error($status);
842
		}
843

    
844
		/*
845
		 * Private Function (added 12 July 05) [beta]
846
		 *   Return Error, Set Last Error, and Die.
847
		 */
848
		function _error($errorNumber = '1') {
849
			switch ($errorNumber) {
850
				case 0:
851
					break;
852
				case 2:
853
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
854
					break;
855
				case 3:
856
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
857
					break;
858
				case 4:
859
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
860
					break;
861
				case 5:
862
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
863
					break;
864
				case 6:
865
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
866
					break;
867
				case 7:
868
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
869
					break;
870
				case 10:
871
					$error = 'phpDynDNS: No change in my IP address and/or 25 days has not passed. Not updating dynamic DNS entry.';
872
					break;
873
				default:
874
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
875
					/* FIXME: $data isn't in scope here */
876
					/* $this->_debug($data); */
877
					break;
878
			}
879
			$this->lastError = $error;
880
			log_error($error);
881
		}
882

    
883
		/*
884
		 * Private Function (added 12 July 05) [beta]
885
		 *   - Detect whether or not IP needs to be updated.
886
		 *      | Written Specifically for pfSense (pfsense.com) may
887
		 *      | work with other systems. pfSense base is FreeBSD.
888
		 */
889
		function _detectChange() {
890
			global $debug;
891

    
892
			if ($debug)
893
				log_error("DynDns: _detectChange() starting.");
894
		
895
			$currentTime = time();
896

    
897
			$wan_ip = $this->_checkIP();
898
			if ($wan_ip == 0) {
899
				log_error("DynDns: Current WAN IP could not be determined, skipping update process.");
900
				return false;
901
			}
902
			$this->_dnsIP = $wan_ip;
903
			$log_error = "DynDns: Current WAN IP: {$wan_ip} ";
904

    
905
			if (file_exists($this->_cacheFile)) {
906
				$contents = file_get_contents($this->_cacheFile);
907
				list($cacheIP,$cacheTime) = explode(':', $contents);
908
				$this->_debug($cacheIP.'/'.$cacheTime);
909
				$initial = false;
910
				$log_error .= "Cached IP: {$cacheIP} ";
911
			} else {
912
				conf_mount_rw();
913
				$cacheIP = '0.0.0.0';
914
				@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
915
				conf_mount_ro();
916
				$cacheTime = $currentTime;
917
				$initial = true;
918
				$log_error .= "No Cached IP found.";
919
			}
920
			log_error($log_error);
921

    
922
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
923
			 *   zoneedit, dyns, ods
924
			 */
925
			$time = '2160000';
926

    
927
			$needs_updating = FALSE;
928
			/* lets determine if the item needs updating */
929
			if ($cacheIP != $wan_ip) {
930
				$needs_updating = true;
931
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
932
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
933
			}
934
			if (($currentTime - $cacheTime) > $time ) {
935
				$needs_updating = true;
936
				$update_reason = "DynDns: More than 25 days.  Updating. ";
937
				$update_reason .= "{$currentTime} - {$cacheTime} > {$time} ";
938
			}
939
			if ($initial == true) {
940
				$needs_updating = true;
941
				$update_reason .= "Inital update. ";
942
			}
943

    
944
			/*   finally if we need updating then store the
945
			 *   new cache value and return true
946
                         */
947
			if ($needs_updating == true) {
948
				log_error("DynDns debug information: {$update_reason}");
949
				return true;
950
			}
951

    
952
			return false;			
953
		}
954

    
955
		/*
956
		 * Private Funcation (added 16 July 05) [beta]
957
		 *   - Writes debug information to a file.
958
		 *   - This function is only called when a unknown response
959
		 *   - status is returned from a DynDNS service provider.
960
		 */
961
		function _debug ($data) {
962
			$string = '\n'.date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data.'\n';
963
			conf_mount_rw();
964
			$file = fopen($this->_debugFile, 'a');
965
			fwrite($file, $string);
966
			fclose($file);
967
			conf_mount_ro();
968
		}
969
		function _checkIP() {
970
			global $debug;
971

    
972
			if ($debug)
973
				log_error("DynDns: _checkIP() starting.");
974

    
975
			$ip_address = find_interface_ip($this->_if);
976
			if (!is_ipaddr($ip_address))
977
				return 0;
978
			$this->_ifIP = $ip_address;
979
			if (is_private_ip($ip_address)) {
980
				$hosttocheck = "checkip.dyndns.org";
981
				$try = 0;
982
				while ($try < 3) {
983
					$checkip = gethostbyname($hosttocheck);
984
					if (is_ipaddr($checkip))
985
						break;
986
					$try++;
987
				}
988
				if ($try >= 3) {
989
					log_error("Dyndns debug information: Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
990
					return 0;
991
				}
992
				$ip_ch = curl_init("http://{$checkip}");
993
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
994
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
995
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
996
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
997
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
998
				$ip_result_page = curl_exec($ip_ch);
999
				curl_close($ip_ch);
1000
				$ip_result_decoded = urldecode($ip_result_page);
1001
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1002
				$ip_address = trim($matches[1]);
1003
				if (is_ipaddr($ip_address))
1004
					log_error("DynDns debug information: {$ip_address} extracted from {$hosttocheck}");
1005
				else {
1006
					log_error("DynDns debug information: IP address could not be extracted from {$hosttocheck}");
1007
					return 0;
1008
				}
1009
			} else
1010
				log_error("DynDns debug information: {$ip_address} extracted from local system.");
1011

    
1012
			return $ip_address;
1013
		}
1014

    
1015
	}
1016

    
1017
?>
(18-18/64)