Project

General

Profile

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

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

    
100
			log_error("DynDns: updatedns() starting");
101

    
102
			$dyndnslck = lock($dnsHost, LOCK_EX);
103

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

    
130
			// Ensure that we where able to lookup the IP
131
			if(!is_ipaddr($this->_ifIP)) {
132
				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}.");
133
				unlock($dyndnslck);
134
				return;
135
			}
136

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

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

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

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

    
358
				case 'staticcling':
359
					$needsIP = FALSE;
360
					curl_setopt($ch, CURLOPT_URL, 'http://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
361
					break;	                    
362
				case 'dnsomatic':
363
					/* Example syntax 
364
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
365
					*/
366
					$needsIP = FALSE;
367
					log_error("DNS-O-Matic: DNS update() starting.");
368
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
369
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
370
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
371
					$server = "https://" . $this->_dnsUser . ":" . $this->_dnsPass . "@updates.dnsomatic.com/nic/update?hostname=";
372
					if($this->_dnsServer)
373
						$server = $this->_dnsServer;
374
					if($this->_dnsPort)
375
						$port = ":" . $this->_dnsPort;
376
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
377
					break;
378
				case 'namecheap':
379
					/* Example:
380
						https://dynamicdns.park-your-domain.com/update?host=[host_name]&domain=[domain.com]&password=[domain_password]&ip=[your_ip]
381
					*/
382
					$needsIP = FALSE;
383
					log_error("Namecheap: DNS update() starting.");
384
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
385
					list($hostname, $domain) = explode(".", $this->_dnsHost, 2);
386
					$server = "https://dynamicdns.park-your-domain.com/update?host={$hostname}&domain={$domain}&password={$this->_dnsPass}&ip={$this->_dnsIP}";
387
					curl_setopt($ch, CURLOPT_URL, $server);
388
					break;
389
				case 'he-net':
390
					$needsIP = FALSE;
391
					log_error("HE.net: DNS update() starting.");
392
					$server = "https://dyn.dns.he.net/nic/update?";
393
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
394
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsHost . ':' . $this->_dnsPass);
395
					curl_setopt($ch, CURLOPT_URL, $server . 'hostname=' . $this->_dnsHost);
396
					break;
397
				default:
398
					break;
399
			}
400
			if ($this->_dnsService != 'ods') {
401
				$data = curl_exec($ch);
402
				$this->_checkStatus($ch, $data);
403
				@curl_close($ch);
404
			}
405
		}
406

    
407
		/*
408
		 * Private Function (added 12 July 2005) [beta]
409
		 *   Retrieve Update Status
410
		 */
411
		function _checkStatus($ch, $data) {
412
			log_error("DynDns: DynDns _checkStatus() starting.");
413
			log_error("DynDns: Current Service: {$this->_dnsService}");
414
			$successful_update = false;
415
			if ($this->_dnsService != 'ods' && @curl_error($ch)) {
416
				$status = "Curl error occurred: " . curl_error($ch);
417
				log_error($status);
418
				$this->status = $status;
419
				return;
420
			}
421
			switch ($this->_dnsService) {
422
				case 'dnsomatic':
423
					if (preg_match('/badauth/i', $data)) {
424
						$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.";
425
					} else if (preg_match('/notfqdn /i', $data)) {
426
						$status = "DNS-O-Matic: The hostname specified is not a fully-qualified domain name. If no hostnames included, notfqdn will be returned once.";
427
					} else if (preg_match('/nohost/i', $data)) {
428
						$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.";
429
					} else if (preg_match('/numhost/i', $data)) {
430
						$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.";	
431
					} else if (preg_match('/abuse/i', $data)) {
432
						$status = "DNS-O-Matic: The hostname is blocked for update abuse.";
433
					} else if (preg_match('/good/i', $data)) {
434
						$status = "DNS-O-Matic: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
435
						$successful_update = true;
436
					} else if (preg_match('/dnserr/i', $data)) {
437
						$status = "DNS-O-Matic: DNS error encountered. Stop updating for 30 minutes.";
438
					} else {
439
						$status = "DNS-O-Matic: (Unknown Response)";
440
						log_error("DNS-O-Matic: PAYLOAD: {$data}");
441
						$this->_debug($data);
442
					}
443
					break;
444
				case 'dyndns':
445
					if (preg_match('/notfqdn/i', $data)) {
446
						$status = "phpDynDNS: (Error) Not A FQDN!";
447
					} else if (preg_match('/nochg/i', $data)) {
448
						$status = "phpDynDNS: (Success) No Change In IP Address";
449
						$successful_update = true;
450
					} else if (preg_match('/good/i', $data)) {
451
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
452
						$successful_update = true;
453
					} else if (preg_match('/noauth/i', $data)) {
454
						$status = "phpDynDNS: (Error) User Authorization Failed";
455
					} else {
456
						$status = "phpDynDNS: (Unknown Response)";
457
						log_error("phpDynDNS: PAYLOAD: {$data}");
458
						$this->_debug($data);
459
					}
460
					break;
461
				case 'dyndns-static':
462
					if (preg_match('/notfqdn/i', $data)) {
463
						$status = "phpDynDNS: (Error) Not A FQDN!";
464
					} else if (preg_match('/nochg/i', $data)) {
465
						$status = "phpDynDNS: (Success) No Change In IP Address";
466
						$successful_update = true;
467
					} else if (preg_match('/good/i', $data)) {
468
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
469
						$successful_update = true;
470
					} else if (preg_match('/noauth/i', $data)) {
471
						$status = "phpDynDNS: (Error) User Authorization Failed";
472
					} else {
473
						$status = "phpDynDNS: (Unknown Response)";
474
						log_error("phpDynDNS: PAYLOAD: {$data}");
475
						$this->_debug($data);
476
					}
477
					break;
478
				case 'dyndns-custom':
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!";
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 'dhs':
496
					break;
497
				case 'noip':
498
					list($ip,$code) = split(":",$data);
499
					switch ($code) {
500
						case 0:
501
							$status = "phpDynDNS: (Success) IP address is current, no update performed.";
502
							$successful_update = true;
503
							break;
504
						case 1:
505
							$status = "phpDynDNS: (Success) DNS hostname update successful.";
506
							$successful_update = true;
507
							break;
508
						case 2:
509
							$status = "phpDynDNS: (Error) Hostname supplied does not exist.";
510
							break;
511
						case 3:
512
							$status = "phpDynDNS: (Error) Invalid Username.";
513
							break;
514
						case 4:
515
							$status = "phpDynDNS: (Error) Invalid Password.";
516
							break;
517
						case 5:
518
							$status = "phpDynDNS: (Error) To many updates sent.";
519
							break;
520
						case 6:
521
							$status = "phpDynDNS: (Error) Account disabled due to violation of No-IP terms of service.";
522
							break;
523
						case 7:
524
							$status = "phpDynDNS: (Error) Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.";
525
							break;
526
						case 8:
527
							$status = "phpDynDNS: (Error) Disabled / Locked Hostname.";
528
							break;
529
						case 9:
530
							$status = "phpDynDNS: (Error) Host updated is configured as a web redirect and no update was performed.";
531
							break;
532
						case 10:
533
							$status = "phpDynDNS: (Error) Group supplied does not exist.";
534
							break;
535
						case 11:
536
							$status = "phpDynDNS: (Success) DNS group update is successful.";
537
							$successful_update = true;
538
							break;
539
						case 12:
540
							$status = "phpDynDNS: (Success) DNS group is current, no update performed.";
541
							$successful_update = true;
542
							break;
543
						case 13:
544
							$status = "phpDynDNS: (Error) Update client support not available for supplied hostname or group.";
545
							break;
546
						case 14:
547
							$status = "phpDynDNS: (Error) Hostname supplied does not have offline settings configured.";
548
							break;
549
						case 99:
550
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
551
							break;
552
						case 100:
553
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
554
							break;
555
						default:
556
							$status = "phpDynDNS: (Unknown Response)";
557
							$this->_debug("Unknown Response: ".$data);
558
							break;
559
					}
560
					break;
561
				case 'easydns':
562
					if (preg_match('/NOACCESS/i', $data)) {
563
						$status = "phpDynDNS: (Error) Authentication Failed: Username and/or Password was Incorrect.";
564
					} else if (preg_match('/NOSERVICE/i', $data)) {
565
						$status = "phpDynDNS: (Error) No Service: Dynamic DNS Service has been disabled for this domain.";
566
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
567
						$status = "phpDynDNS: (Error) Illegal Input: Self-Explantory";
568
					} else if (preg_match('/TOOSOON/i', $data)) {
569
						$status = "phpDynDNS: (Error) Too Soon: Not Enough Time Has Elapsed Since Last Update";
570
					} else if (preg_match('/NOERROR/i', $data)) {
571
						$status = "phpDynDNS: (Success) IP Updated Successfully!";
572
						$successful_update = true;
573
					} else {
574
						$status = "phpDynDNS: (Unknown Response)";
575
						log_error("phpDynDNS: PAYLOAD: {$data}");
576
						$this->_debug($data);
577
					}
578
					break;
579
				case 'hn':
580
					/* FIXME: add checks */
581
					break;
582
				case 'zoneedit':
583
					if (preg_match('/799/i', $data)) {
584
						$status = "phpDynDNS: (Error 799) Update Failed!";				
585
					} else if (preg_match('/700/i', $data)) {
586
						$status = "phpDynDNS: (Error 700) Update Failed!";
587
					} else if (preg_match('/200/i', $data)) {
588
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
589
						$successful_update = true;
590
					} else if (preg_match('/201/i', $data)) {
591
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
592
						$successful_update = true;						
593
					} else {
594
						$status = "phpDynDNS: (Unknown Response)";
595
						log_error("phpDynDNS: PAYLOAD: {$data}");
596
						$this->_debug($data);
597
					}
598
					break;
599
				case 'dyns':
600
					if (preg_match("/400/i", $data)) {
601
						$status = "phpDynDNS: (Error) Bad Request - The URL was malformed. Required parameters were not provided.";
602
					} else if (preg_match('/402/i', $data)) {
603
						$status = "phpDynDNS: (Error) Update Too Soon - You have tried updating to quickly since last change.";
604
					} else if (preg_match('/403/i', $data)) {
605
						$status = "phpDynDNS: (Error) Database Error - There was a server-sided database error.";
606
					} else if (preg_match('/405/i', $data)) {
607
						$status = "phpDynDNS: (Error) Hostname Error - The hostname (".$this->_dnsHost.") doesn't belong to you.";
608
					} else if (preg_match('/200/i', $data)) {
609
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
610
						$successful_update = true;
611
					} else {
612
						$status = "phpDynDNS: (Unknown Response)";
613
						log_error("phpDynDNS: PAYLOAD: {$data}");
614
						$this->_debug($data);
615
					}
616
					break;
617
				case 'ods':
618
					if (preg_match("/299/i", $data)) {
619
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
620
						$successful_update = true;
621
					} else {
622
						$status = "phpDynDNS: (Unknown Response)";
623
						log_error("phpDynDNS: PAYLOAD: {$data}");
624
						$this->_debug($data);
625
					}
626
					break;
627
				case 'freedns':
628
					if (preg_match("/has not changed./i", $data)) {
629
						$status = "phpDynDNS: (Success) No Change In IP Address";
630
						$successful_update = true;
631
					} else if (preg_match("/Updated/i", $data)) {
632
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
633
						$successful_update = true;
634
					} else {
635
						$status = "phpDynDNS: (Unknown Response)";
636
						log_error("phpDynDNS: PAYLOAD: {$data}");
637
						$this->_debug($data);
638
					} 
639
					break;
640
				case 'dnsexit':
641
					if (preg_match("/is the same/i", $data)) {
642
						$status = "phpDynDns: (Success) No Change In IP Address";
643
						$successful_update = true;
644
					} else if (preg_match("/Success/i", $data)) {
645
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
646
						$successful_update = true;
647
					} else {
648
						$status = "phpDynDNS: (Unknown Response)";
649
                                                log_error("phpDynDNS: PAYLOAD: {$data}");
650
                                                $this->_debug($data);
651
					}
652
					break;
653
				case 'loopia':
654
					if (preg_match("/nochg/i", $data)) {
655
						$status = "phpDynDNS: (Success) No Change In IP Address";
656
						$successful_update = true;
657
					} else if (preg_match("/good/i", $data)) {
658
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
659
						$successful_update = true;
660
					} else if (preg_match('/badauth/i', $data)) {
661
						$status = "phpDynDNS: (Error) User Authorization Failed";
662
					} else {
663
						$status = "phpDynDNS: (Unknown Response)";
664
						log_error("phpDynDNS: PAYLOAD: {$data}");
665
						$this->_debug($data);
666
					}
667
					break;
668
				case 'opendns':
669
					if (preg_match('/badauth/i', $data)) {
670
						$status = "phpDynDNS: (Error) Not a valid username or password!";
671
					} else if (preg_match('/nohost/i', $data)) {
672
						$status = "phpDynDNS: (Error) Hostname you are trying to update does not exist.";
673
						$successful_update = true;
674
					} else if (preg_match('/good/i', $data)) {
675
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
676
						$successful_update = true;
677
					} else if (preg_match('/yours/i', $data)) {
678
						$status = "phpDynDNS: (Error) hostname specified exists, but not under the username specified.";
679
					} else if (preg_match('/abuse/i', $data)) {
680
						$status = "phpDynDns: (Error) Updating to frequently, considered abuse.";
681
					} else {
682
						$status = "phpDynDNS: (Unknown Response)";
683
						log_error("phpDynDNS: PAYLOAD: {$data}");
684
						$this->_debug($data);
685
					}
686
					break;
687
                 case 'staticcling':
688
					if (preg_match("/invalid ip/i", $data)) {
689
					        $status = "phpDynDNS: (Error) Bad Request - The IP provided was invalid.";
690
					} else if (preg_match('/required info missing/i', $data)) {
691
					        $status = "phpDynDNS: (Error) Bad Request - Required parameters were not provided.";
692
					} else if (preg_match('/invalid characters/i', $data)) {
693
					        $status = "phpDynDNS: (Error) Bad Request - Illegal characters in either the username or the password.";
694
					} else if (preg_match('/bad password/i', $data)) {
695
					        $status = "phpDynDNS: (Error) Invalid password.";
696
					} else if (preg_match('/account locked/i', $data)) {
697
					        $status = "phpDynDNS: (Error) This account has been administratively locked.";
698
					} else if (preg_match('/update too frequent/i', $data)) {
699
					        $status = "phpDynDNS: (Error) Updating too frequently.";
700
					} else if (preg_match('/DB error/i', $data)) {
701
					        $status = "phpDynDNS: (Error) Server side error.";
702
					} else if (preg_match('/success/i', $data)) {
703
					        $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
704
					        $successful_update = true;
705
					} else {
706
					        $status = "phpDynDNS: (Unknown Response)";
707
					        log_error("phpDynDNS: PAYLOAD: {$data}");
708
					        $this->_debug($data);
709
					}
710
					break;
711
				case 'namecheap':
712
					$tmp = str_replace("^M", "", $data);
713
					$ncresponse = @xml2array($tmp);
714
					if (preg_match("/internal server error/i", $data)) {
715
						$status = "phpDynDNS: (Error) Server side error.";
716
					} else if ($ncresponse['interface-response']['ErrCount'] === "0") {
717
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
718
						$successful_update = true;
719
					} else if (is_numeric($ncresponse['interface-response']['ErrCount']) && ($ncresponse['interface-response']['ErrCount'] > 0)) {
720
						$status = "phpDynDNS: (Error) " . implode(", ", $ncresponse["interface-response"]["errors"]);
721
						$successful_update = true;
722
					} else {
723
						$status = "phpDynDNS: (Unknown Response)";
724
						log_error("phpDynDNS: PAYLOAD: {$data}");
725
						$this->_debug($data);
726
					}
727
					break;
728
					
729
				case 'he-net':
730
					if (preg_match("/badip/i", $data)) {
731
					        $status = "phpDynDNS: (Error) Bad Request - The IP provided was invalid.";
732
					} else if (preg_match('/nohost/i', $data)) {
733
					        $status = "phpDynDNS: (Error) Bad Request - A hostname was not provided.";
734
					} else if (preg_match('/badauth/i', $data)) {
735
					        $status = "phpDynDNS: (Error) Invalid username or password.";
736
					} else if (preg_match('/good/i', $data)) {
737
					        $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
738
					        $successful_update = true;
739
					} else if (preg_match('/nochg/i', $data)) {
740
							$status = "phpDynDNS: (Success) No Change In IP Address.";
741
							$successful_update = true;
742
					} else {
743
					        $status = "phpDynDNS: (Unknown Response)";
744
					        log_error("phpDynDNS: PAYLOAD: {$data}");
745
					        $this->_debug($data);
746
					}
747
					break;
748
			}
749
			
750
			if($successful_update == true) {
751
				/* Write WAN IP to cache file */
752
				$wan_ip = $this->_checkIP();
753
				conf_mount_rw();
754
				if ($wan_ip > 0) {
755
					$currentTime = time();				  
756
					log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
757
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
758
				} else
759
					@unlink($this->_cacheFile);
760
				conf_mount_ro();
761
			}
762
			$this->status = $status;
763
			log_error($status);
764
		}
765

    
766
		/*
767
		 * Private Function (added 12 July 05) [beta]
768
		 *   Return Error, Set Last Error, and Die.
769
		 */
770
		function _error($errorNumber = '1') {
771
			switch ($errorNumber) {
772
				case 0:
773
					break;
774
				case 2:
775
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
776
					break;
777
				case 3:
778
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
779
					break;
780
				case 4:
781
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
782
					break;
783
				case 5:
784
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
785
					break;
786
				case 6:
787
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
788
					break;
789
				case 7:
790
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
791
					break;
792
				case 10:
793
					$error = 'phpDynDNS: No change in my IP address and/or 25 days has not passed. Not updating dynamic DNS entry.';
794
					break;
795
				default:
796
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
797
					/* FIXME: $data isn't in scope here */
798
					/* $this->_debug($data); */
799
					break;
800
			}
801
			$this->lastError = $error;
802
			log_error($error);
803
		}
804

    
805
		/*
806
		 * Private Function (added 12 July 05) [beta]
807
		 *   - Detect whether or not IP needs to be updated.
808
		 *      | Written Specifically for pfSense (pfsense.com) may
809
		 *      | work with other systems. pfSense base is FreeBSD.
810
		 */
811
		function _detectChange() {
812
			global $debug;
813

    
814
			if ($debug)
815
				log_error("DynDns: _detectChange() starting.");
816
		
817
			$currentTime = time();
818

    
819
			$wan_ip = $this->_checkIP();
820
			if ($wan_ip == 0) {
821
				log_error("DynDns: Current WAN IP could not be determined, skipping update process.");
822
				return false;
823
			}
824
			$this->_dnsIP = $wan_ip;
825
			$log_error = "DynDns: Current WAN IP: {$wan_ip} ";
826

    
827
			if (file_exists($this->_cacheFile)) {
828
				$contents = file_get_contents($this->_cacheFile);
829
				list($cacheIP,$cacheTime) = split(':', $contents);
830
				$this->_debug($cacheIP.'/'.$cacheTime);
831
				$initial = false;
832
				$log_error .= "Cached IP: {$cacheIP} ";
833
			} else {
834
				conf_mount_rw();
835
				$cacheIP = '0.0.0.0';
836
				@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
837
				conf_mount_ro();
838
				$cacheTime = $currentTime;
839
				$initial = true;
840
				$log_error .= "No Cached IP found.";
841
			}
842
			log_error($log_error);
843

    
844
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
845
			 *   zoneedit, dyns, ods
846
			 */
847
			$time = '2160000';
848

    
849
			$needs_updating = FALSE;
850
			/* lets determine if the item needs updating */
851
			if ($cacheIP != $wan_ip) {
852
				$needs_updating = true;
853
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
854
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
855
			}
856
			if (($currentTime - $cacheTime) > $time ) {
857
				$needs_updating = true;
858
				$update_reason = "DynDns: More than 25 days.  Updating. ";
859
				$update_reason .= "{$currentTime} - {$cacheTime} > {$time} ";
860
			}
861
			if ($initial == true) {
862
				$needs_updating = true;
863
				$update_reason .= "Inital update. ";
864
			}
865

    
866
			/*   finally if we need updating then store the
867
			 *   new cache value and return true
868
                         */
869
			if ($needs_updating == true) {
870
				log_error("DynDns debug information: {$update_reason}");
871
				return true;
872
			}
873

    
874
			return false;			
875
		}
876

    
877
		/*
878
		 * Private Funcation (added 16 July 05) [beta]
879
		 *   - Writes debug information to a file.
880
		 *   - This function is only called when a unknown response
881
		 *   - status is returned from a DynDNS service provider.
882
		 */
883
		function _debug ($data) {
884
			$string = '\n'.date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data.'\n';
885
			conf_mount_rw();
886
			$file = fopen($this->_debugFile, 'a');
887
			fwrite($file, $string);
888
			fclose($file);
889
			conf_mount_ro();
890
		}
891
		function _checkIP() {
892
			global $debug;
893

    
894
			if ($debug)
895
				log_error("DynDns: _checkIP() starting.");
896

    
897
			$ip_address = find_interface_ip($this->_if);
898
			if (!is_ipaddr($ip_address))
899
				return 0;
900
			$this->_ifIP = $ip_address;
901
			if (is_private_ip($ip_address)) {
902
				$hosttocheck = "checkip.dyndns.org";
903
				$try = 0;
904
				while ($try < 3) {
905
					$checkip = gethostbyname($hosttocheck);
906
					if (is_ipaddr($checkip))
907
						break;
908
					$try++;
909
				}
910
				if ($try >= 3) {
911
					log_error("Dyndns debug information: Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
912
					return 0;
913
				}
914
				$ip_ch = curl_init("http://{$checkip}");
915
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
916
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
917
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
918
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
919
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
920
				$ip_result_page = curl_exec($ip_ch);
921
				curl_close($ip_ch);
922
				$ip_result_decoded = urldecode($ip_result_page);
923
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
924
				$ip_address = trim($matches[1]);
925
				if (is_ipaddr($ip_address))
926
					log_error("DynDns debug information: {$ip_address} extracted from {$hosttocheck}");
927
				else {
928
					log_error("DynDns debug information: IP address could not be extracted from {$hosttocheck}");
929
					return 0;
930
				}
931
			} else
932
				log_error("DynDns debug information: {$ip_address} extracted from local system.");
933

    
934
			return $ip_address;
935
		}
936

    
937
	}
938

    
939
?>
(17-17/61)