Project

General

Profile

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

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

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

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

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

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

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

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

    
811
		/*
812
		 * Private Function (added 12 July 05) [beta]
813
		 *   Return Error, Set Last Error, and Die.
814
		 */
815
		function _error($errorNumber = '1') {
816
			switch ($errorNumber) {
817
				case 0:
818
					break;
819
				case 2:
820
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
821
					break;
822
				case 3:
823
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
824
					break;
825
				case 4:
826
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
827
					break;
828
				case 5:
829
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
830
					break;
831
				case 6:
832
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
833
					break;
834
				case 7:
835
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
836
					break;
837
				case 10:
838
					$error = 'phpDynDNS: No change in my IP address and/or 25 days has not passed. Not updating dynamic DNS entry.';
839
					break;
840
				default:
841
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
842
					/* FIXME: $data isn't in scope here */
843
					/* $this->_debug($data); */
844
					break;
845
			}
846
			$this->lastError = $error;
847
			log_error($error);
848
		}
849

    
850
		/*
851
		 * Private Function (added 12 July 05) [beta]
852
		 *   - Detect whether or not IP needs to be updated.
853
		 *      | Written Specifically for pfSense (pfsense.com) may
854
		 *      | work with other systems. pfSense base is FreeBSD.
855
		 */
856
		function _detectChange() {
857
			
858
			log_error("DynDns: _detectChange() starting.");
859
		
860
			$currentTime = time();
861

    
862
			$wan_ip = $this->_checkIP();
863
			if ($wan_ip == 0) {
864
				log_error("DynDns: Current WAN IP could not be determined.");
865
				return true;
866
			}
867
			$this->_dnsIP = $wan_ip;
868
			log_error("DynDns: Current WAN IP: {$wan_ip}");
869

    
870
			if (file_exists($this->_cacheFile)) {
871
				$contents = file_get_contents($this->_cacheFile);
872
				list($cacheIP,$cacheTime) = split(':', $contents);
873
				$this->_debug($cacheIP.'/'.$cacheTime);
874
				$initial = false;
875
				log_error("DynDns: Cached IP: {$cacheIP}");
876
			} else {
877
				conf_mount_rw();
878
				$cacheIP = '0.0.0.0';
879
				@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
880
				conf_mount_ro();
881
				$cacheTime = $currentTime;
882
				$initial = true;
883
				log_error("DynDns: No Cached IP found.");
884
			}
885

    
886
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
887
			 *   zoneedit, dyns, ods
888
			 */
889
			$time = '2160000';
890

    
891
			$needs_updating = FALSE;
892
			/* lets determine if the item needs updating */
893
			if ($cacheIP != $wan_ip) {
894
				$needs_updating = true;
895
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
896
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
897
			}
898
			if (($currentTime - $cacheTime) > $time ) {
899
				$needs_updating = true;
900
				$update_reason = "DynDns: More than 25 days.  Updating. ";
901
				$update_reason .= "{$currentTime} - {$cacheTime} > {$time} ";
902
			}
903
			if ($initial == true) {
904
				$needs_updating = true;
905
				$update_reason .= "Inital update. ";
906
			}
907

    
908
			/*   finally if we need updating then store the
909
			 *   new cache value and return true
910
                         */
911
			if ($needs_updating == true) {
912
				log_error("DynDns debug information: {$update_reason}");
913
				return true;
914
			}
915

    
916
			return false;			
917
		}
918

    
919
		/*
920
		 * Private Funcation (added 16 July 05) [beta]
921
		 *   - Writes debug information to a file.
922
		 *   - This function is only called when a unknown response
923
		 *   - status is returned from a DynDNS service provider.
924
		 */
925
		function _debug ($data) {
926
			$string = '\n'.date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data.'\n';
927
			conf_mount_rw();
928
			$file = fopen($this->_debugFile, 'a');
929
			fwrite($file, $string);
930
			fclose($file);
931
			conf_mount_ro();
932
		}
933
		function _checkIP() {
934

    
935
			log_error("DynDns: _checkIP() starting.");
936

    
937
			$ip_address = find_interface_ip($this->_if);
938
			$this->_ifIP = $ip_address;
939
			if (is_private_ip($ip_address)) {
940
				$hosttocheck = "checkip.dyndns.org";
941
				$try = 0;
942
				while ($try < 3) {
943
					$checkip = gethostbyname($hosttocheck);
944
					if (is_ipaddr($checkip))
945
						break;
946
					$try++;
947
				}
948
				if ($try >= 3) {
949
					log_error("Dyndns debug information: Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
950
					return 0;
951
				}
952
				$ip_ch = curl_init("http://{$checkip}");
953
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
954
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
955
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
956
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
957
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 60);
958
				$ip_result_page = curl_exec($ip_ch);
959
				curl_close($ip_ch);
960
				$ip_result_decoded = urldecode($ip_result_page);
961
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
962
				$ip_address = trim($matches[1]);
963
				if (is_ipaddr($ip_address))
964
					log_error("DynDns debug information: {$ip_address} extracted from {$hosttocheck}");
965
				else {
966
					log_error("DynDns debug information: IP address could not be extracted from {$hosttocheck}");
967
					return 0;
968
				}
969
			} else
970
				log_error("DynDns debug information: {$ip_address} extracted from local system.");
971

    
972
			return $ip_address;
973
		}
974

    
975
	}
976

    
977
?>
(17-17/61)