Project

General

Profile

Download (34.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
	/*
3
	 * PHP.updateDNS (pfSense version)
4
	 *
5
	 * +====================================================+
6
	 *  Services Supported:
7
	 *    - DynDns (dyndns.org) [dynamic, static, custom]
8
	 *    - DHSDns (dhs.org)
9
	 *    - No-IP (no-ip.com)
10
	 *    - EasyDNS (easydns.com)
11
	 *    - DHS (www.dhs.org)
12
	 *    - HN (hn.org) -- incomplete checking!
13
	 *    - DynS (dyns.org)
14
	 *    - ZoneEdit (zoneedit.com)
15
	 *    - FreeDNS (freedns.afraid.org)
16
	 *    - Loopia (loopia.se)
17
	 *    - StaticCling (staticcling.org)
18
	 *    - DNSexit (dnsexit.com)
19
	 *    - OpenDNS (opendns.com)
20
	 *    - Namecheap (namecheap.com)
21
	 * +----------------------------------------------------+
22
	 *  Requirements:
23
	 *    - PHP version 4.0.2 or higher with CURL Library
24
	 * +----------------------------------------------------+
25
	 *  Public Functions
26
	 *    - updatedns()
27
	 *
28
	 *  Private Functions
29
	 *    - _update()
30
	 *    - _checkStatus()
31
	 *    - _error()
32
	 *    - _detectChange()
33
	 *    - _debug()
34
	 *    - _checkIP()
35
	 * +----------------------------------------------------+
36
	 *  DynDNS Dynamic - Last Tested: 12 July 2005
37
	 *  DynDNS Static  - Last Tested: NEVER
38
	 *  DynDNS Custom  - Last Tested: NEVER
39
	 *  No-IP          - Last Tested: 20 July 2008
40
	 *  HN.org         - Last Tested: 12 July 2005
41
	 *  EasyDNS        - Last Tested: 20 July 2008
42
	 *  DHS            - Last Tested: 12 July 2005
43
	 *  ZoneEdit       - Last Tested: NEVER
44
	 *  Dyns           - Last Tested: NEVER
45
	 *  ODS            - Last Tested: 02 August 2005
46
	 *  FreeDNS        - Last Tested: NEVER
47
	 *  Loopia         - Last Tested: NEVER
48
	 *  StaticCling    - Last Tested: 27 April 2006
49
	 *  DNSexit	   - Last Tested: 20 July 2008
50
	 *  OpenDNS	   - Last Tested: 4 August 2008
51
	 *  Namecheap	   - Last Tested: 31 August 2010
52
	 * +====================================================+
53
	 *
54
	 * @author 	E.Kristensen
55
	 * @link    	http://www.idylldesigns.com/projects/phpdns/
56
	 * @version 	0.8
57
	 * @updated	13 October 05 at 21:02:42 GMT
58
	 *
59
	 * DNSexit/OpenDNS support and multiwan extension for pfSense by Ermal Lu?i
60
	 *
61
	 */
62

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

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

    
126
			// Ensure that we where able to lookup the IP
127
			if(!is_ipaddr($this->_ifIP)) {
128
				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}.");
129
				return;
130
			}
131

    
132
			$this->_debugID = rand(1000000, 9999999);
133
			
134
			if ($this->_detectChange() == false) {
135
				$this->_error(10);
136
			} else {
137
				switch ($this->_dnsService) {
138
				case 'dnsomatic':
139
				case 'dyndns':
140
				case 'dyndns-static':
141
				case 'dyndns-custom':
142
				case 'dhs':
143
				case 'noip':
144
				case 'easydns':
145
				case 'hn':
146
				case 'zoneedit':
147
				case 'dyns':
148
				case 'ods':
149
				case 'freedns':
150
				case 'loopia':
151
				case 'staticcling':
152
				case 'dnsexit':
153
				case 'opendns':
154
				case 'namecheap':
155
					$this->_update();	
156
					break;
157
				default:
158
					$this->_error(6);
159
					break;
160
				}
161
			}
162
		}
163
			
164
		/*
165
		 * Private Function (added 12 July 05) [beta]
166
		 *   Send Update To Selected Service.
167
		 */
168
		function _update() {
169
		
170
			log_error("DynDns: DynDns _update() starting.");
171
		
172
			if ($this->_dnsService != 'ods') {
173
				$ch = curl_init();
174
				curl_setopt($ch, CURLOPT_HEADER, 0);
175
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
176
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
177
				curl_setopt($ch, CURLOPT_INTERFACE, $this->_ifIP);
178
				curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Completely empirical
179
			}
180

    
181
			switch ($this->_dnsService) {
182
				case 'dyndns':
183
				case 'dyndns-static':
184
				case 'dyndns-custom':
185
					$needsIP = FALSE;
186
					//log_error("DynDns: DynDns _update() starting. Dynamic");
187
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
188
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
189
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
190
					$server = "https://members.dyndns.org/nic/update";
191
					$port = "";
192
					if($this->_dnsServer)
193
						$server = $this->_dnsServer;
194
					if($this->_dnsPort)
195
						$port = ":" . $this->_dnsPort;
196
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
197
					$data = curl_exec($ch);
198
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
199
					curl_close($ch);
200
					$this->_checkStatus($data);
201
					break;
202
				case 'dhs':
203
					$needsIP = TRUE;
204
					$post_data['hostscmd'] = 'edit';
205
					$post_data['hostscmdstage'] = '2';
206
					$post_data['type'] = '4';
207
					$post_data['updatetype'] = 'Online';
208
					$post_data['mx'] = $this->_dnsMX;
209
					$post_data['mx2'] = '';
210
					$post_data['txt'] = '';
211
					$post_data['offline_url'] = '';
212
					$post_data['cloak'] = 'Y';
213
					$post_data['cloak_title'] = '';
214
					$post_data['ip'] = $this->_dnsIP;
215
					$post_data['domain'] = 'dyn.dhs.org';
216
					$post_data['hostname'] = $this->_dnsHost;
217
					$post_data['submit'] = 'Update';
218
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
219
					$server = "https://members.dhs.org/nic/hosts";
220
					$port = "";
221
					if($this->_dnsServer)
222
						$server = $this->_dnsServer;
223
					if($this->_dnsPort)
224
						$port = ":" . $this->_dnsPort;					
225
					curl_setopt($ch, CURLOPT_URL, '{$server}{$port}');
226
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
227
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
228
					$data = curl_exec($ch);
229
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
230
					curl_close($ch);
231
					$this->_checkStatus($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
					$data = curl_exec($ch);
244
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
245
					curl_close($ch);
246
					$this->_checkStatus($data);
247
					break;
248
				case 'easydns':
249
					$needsIP = TRUE;
250
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
251
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
252
					$server = "http://members.easydns.com/dyn/dyndns.php";
253
					$port = "";
254
					if($this->_dnsServer)
255
						$server = $this->_dnsServer;
256
					if($this->_dnsPort)
257
						$port = ":" . $this->_dnsPort;
258
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard=' . $this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=' . $this->_dnsBackMX);
259
					$data = curl_exec($ch);
260
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
261
					curl_close($ch);
262
					$this->_checkStatus($data);
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
					$data = curl_exec($ch);
276
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
277
					curl_close($ch);
278
					$this->_checkStatus($data);
279
					break;
280
				case 'zoneedit':
281
					$needsIP = FALSE;
282
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
283
					curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
284
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
285

    
286
					$server = "https://dynamic.zoneedit.com/auth/dynamic.html";
287
					$port = "";
288
					if($this->_dnsServer)
289
						$server = $this->_dnsServer;
290
					if($this->_dnsPort)
291
						$port = ":" . $this->_dnsPort;
292
					curl_setopt($ch, CURLOPT_URL, "{$server}{$port}?host=" .$this->_dnsHost);
293

    
294
					$data = curl_exec($ch);
295
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
296
					curl_close($ch);
297
					$this->_checkStatus($data);
298
					break;
299
				case 'dyns':
300
					$needsIP = FALSE;
301
					$server = "http://www.dyns.cx/postscript011.php";
302
					$port = "";
303
					if($this->_dnsServer)
304
						$server = $this->_dnsServer;
305
					if($this->_dnsPort)
306
						$port = ":" . $this->_dnsPort;					
307
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&password=' . $this->_dnsPass . '&host=' . $this->_dnsHost);
308
					$data = curl_exec($ch);
309
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
310
					curl_close($ch);
311
					$this->_checkStatus($data);
312
					break;
313
				case 'ods':
314
					$needsIP = FALSE;
315
					$misc_errno = 0;
316
					$misc_error = "";
317
					$server = "ods.org";
318
					$port = "";
319
					if($this->_dnsServer)
320
						$server = $this->_dnsServer;
321
					if($this->_dnsPort)
322
						$port = ":" . $this->_dnsPort;						
323
					$this->con['socket'] = fsockopen("{$server}{$port}", "7070", $misc_errno, $misc_error, 30);
324
					/* Check that we have connected */
325
					if (!$this->con['socket']) {
326
						print "error! could not connect.";
327
						break;
328
					}
329
					/* Here is the loop. Read the incoming data (from the socket connection) */
330
					while (!feof($this->con['socket'])) {
331
						$this->con['buffer']['all'] = trim(fgets($this->con['socket'], 4096));
332
						$code = substr($this->con['buffer']['all'], 0, 3);
333
						sleep(1);
334
						switch($code) {
335
							case 100:
336
								fputs($this->con['socket'], "LOGIN ".$this->_dnsUser." ".$this->_dnsPass."\n");
337
								break;
338
							case 225:
339
								fputs($this->con['socket'], "DELRR ".$this->_dnsHost." A\n");
340
								break;
341
							case 901:
342
								fputs($this->con['socket'], "ADDRR ".$this->_dnsHost." A ".$this->_dnsIP."\n");
343
								break;
344
							case 795:
345
								fputs($this->con['socket'], "QUIT\n");
346
								break;
347
						}
348
					}
349
					$this->_checkStatus($code);
350
					break;
351
				case 'freedns':
352
					$needIP = FALSE;
353
					curl_setopt($ch, CURLOPT_URL, 'http://freedns.afraid.org/dynamic/update.php?' . $this->_dnsHost);
354
					$data = curl_exec($ch);
355
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
356
					curl_close($ch);
357
					$this->_checkStatus($data);
358
					break;
359
				case 'dnsexit':
360
					$needsIP = TRUE;
361
					curl_setopt($ch, CURLOPT_URL, 'http://www.dnsexit.com/RemoteUpdate.sv?login='.$this->_dnsUser. '&password='.$this->_dnsPass.'&host='.$this->_dnsHost.'&myip='.$this->_dnsIP);
362
					$data = curl_exec($ch);
363
					if (@curl_error($ch)) log_error("Curl error occurred:" . curl_error($ch));
364
					curl_close($ch);
365
					$this->_checkStatus($data);
366
					break;
367
				case 'loopia':
368
					$needsIP = TRUE;
369
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
370
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
371
					curl_setopt($ch, CURLOPT_URL, 'https://dns.loopia.se/XDynDNSServer/XDynDNS.php?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP);
372
					$data = curl_exec($ch);
373
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
374
					curl_close($ch);
375
					$this->_checkStatus($data);
376
					break;
377
				case 'opendns':
378
					$needsIP = FALSE;
379
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
380
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
381
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
382
					$server = "https://updates.opendns.com/nic/update?hostname=". $this->_dnsHost;
383
					$port = "";
384
					if($this->_dnsServer)
385
						$server = $this->_dnsServer;
386
					if($this->_dnsPort)
387
						$port = ":" . $this->_dnsPort;
388
					curl_setopt($ch, CURLOPT_URL, $server .$port);
389
					$data = curl_exec($ch);
390
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
391
					curl_close($ch);
392
					$this->_checkStatus($data);
393
					break;
394

    
395
				case 'staticcling':
396
					$needsIP = FALSE;
397
					curl_setopt($ch, CURLOPT_URL, 'http://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
398
					$data = curl_exec($ch);
399
					if (@curl_error($ch)) log_error("Curl error occured: " . curl_error($ch));
400
					curl_close($ch);
401
					$this->_checkStatus($data);
402
					break;	                    
403
				case 'dnsomatic':
404
					/* Example syntax 
405
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
406
					*/
407
					$needsIP = FALSE;
408
					log_error("DNS-O-Matic: DNS update() starting.");
409
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
410
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
411
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
412
					$server = "https://" . $this->_dnsUser . ":" . $this->_dnsPass . "@updates.dnsomatic.com/nic/update?hostname=";
413
					if($this->_dnsServer)
414
						$server = $this->_dnsServer;
415
					if($this->_dnsPort)
416
						$port = ":" . $this->_dnsPort;
417
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
418
					$data = curl_exec($ch);
419
					if (@curl_error($ch)) log_error("Request completed. DNS-O-Matic reported: " . curl_error($ch));
420
					curl_close($ch);
421
					$this->_checkStatus($data);
422
					break;
423
				case 'namecheap':
424
					/* Example:
425
						https://dynamicdns.park-your-domain.com/update?host=[host_name]&domain=[domain.com]&password=[domain_password]&ip=[your_ip]
426
					*/
427
					$needsIP = FALSE;
428
					log_error("Namecheap: DNS update() starting.");
429
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
430
					list($hostname, $domain) = explode(".", $this->_dnsHost, 2);
431
					$server = "https://dynamicdns.park-your-domain.com/update?host={$hostname}&domain={$domain}&password={$this->_dnsPass}&ip={$this->_dnsIP}";
432
					curl_setopt($ch, CURLOPT_URL, $server);
433
					$data = curl_exec($ch);
434
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
435
					curl_close($ch);
436
					$this->_checkStatus($data);
437
				default:
438
					break;
439
			}
440
		}
441

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

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

    
813
		/*
814
		 * Private Function (added 12 July 05) [beta]
815
		 *   - Detect whether or not IP needs to be updated.
816
		 *      | Written Specifically for pfSense (pfsense.com) may
817
		 *      | work with other systems. pfSense base is FreeBSD.
818
		 */
819
		function _detectChange() {
820
			
821
			log_error("DynDns: _detectChange() starting.");
822
		
823
			$currentTime = time();
824

    
825
			$wan_ip = $this->_checkIP();
826
			$this->_dnsIP = $wan_ip;
827
			log_error("DynDns: Current WAN IP: {$wan_ip}");
828

    
829
			if (file_exists($this->_cacheFile)) {
830
				if(file_exists($this->_cacheFile))
831
					$contents = file_get_contents($this->_cacheFile);
832
				else
833
					$contents = "";
834
				list($cacheIP,$cacheTime) = split(':', $contents);
835
				$this->_debug($cacheIP.'/'.$cacheTime);
836
				$initial = false;
837
				log_error("DynDns: Cached IP: {$cacheIP}");
838
			} else {
839
				conf_mount_rw();
840
				$file = fopen($this->_cacheFile, 'w');
841
				fwrite($file, '0.0.0.0:'.$currentTime);
842
				fclose($file);
843
				conf_mount_ro();
844
				$cacheIP = '0.0.0.0';
845
				$cacheTime = $currentTime;
846
				$initial = true;
847
				log_error("DynDns: No Cached IP found.");
848
			}
849

    
850
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
851
			 *   zoneedit, dyns, ods
852
			 */
853
			$time = '2160000';
854

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

    
872
			/*   finally if we need updating then store the
873
			 *   new cache value and return true
874
                         */
875
			if ($needs_updating == true) {
876
				log_error("DynDns debug information: {$update_reason}");
877
				return true;
878
			}
879

    
880
			return false;			
881
		}
882

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

    
899
			log_error("DynDns: _checkIP() starting.");
900

    
901
			$ip_address = find_interface_ip($this->_if);
902
			$this->_ifIP = $ip_address;
903
			if (is_private_ip($ip_address)) {
904
				$hosttocheck = "checkip.dyndns.org";
905
				$checkip = gethostbyname($hosttocheck);
906
				$ip_ch = curl_init("http://{$checkip}");
907
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
908
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
909
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
910
				$ip_result_page = curl_exec($ip_ch);
911
				curl_close($ip_ch);
912
				$ip_result_decoded = urldecode($ip_result_page);
913
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
914
				$ip_address = trim($matches[1]);
915
				log_error("DynDns debug information: {$ip_address} extracted from {$hosttocheck}");
916
			} else
917
				log_error("DynDns debug information: {$ip_address} extracted from local system.");
918

    
919
			return $ip_address;
920
		}
921

    
922
	}
923

    
924
?>
(14-14/54)