Project

General

Profile

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

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

    
96
			log_error("DynDns: updatedns() starting");
97
			
98
			if (!$dnsService) $this->_error(2);
99
			if (!($dnsService == 'freedns')) {
100

    
101
				/* all services except freedns use these */
102

    
103
				if (!$dnsUser) $this->_error(3);
104
				if (!$dnsPass) $this->_error(4);
105
				if (!$dnsHost) $this->_error(5);
106
			} else {
107

    
108
				/* freedns needs this */
109

    
110
				if (!$dnsHost) $this->_error(5);
111
			}
112
			
113
			$this->_dnsService = strtolower($dnsService);
114
			$this->_dnsUser = $dnsUser;
115
			$this->_dnsPass = $dnsPass;
116
			$this->_dnsHost = $dnsHost;
117
			$this->_dnsServer = $dnsServer;
118
			$this->_dnsPort = $dnsPort;
119
			$this->_dnsWildcard = $dnsWildcard;
120
			$this->_dnsMX = $dnsMX;
121
			$this->_if = get_real_interface($dnsIf);
122
			$this->_ifIP = get_interface_ip($dnsIf);
123
			//$this->_dnsIP = get_interface_ip($dnsIf);
124

    
125
			// Ensure that we where able to lookup the IP
126
			if(!$this->_ifIP)
127
				log_error("There was an error trying to determine the IP for interface - {$dnsIf}({$this->_if}).");
128

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

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

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

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

    
391
				case 'staticcling':
392
					$needsIP = FALSE;
393
					curl_setopt($ch, CURLOPT_URL, 'http://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
394
					$data = curl_exec($ch);
395
					if (@curl_error($ch)) log_error("Curl error occured: " . curl_error($ch));
396
					curl_close($ch);
397
					$this->_checkStatus($data);
398
					break;	                    
399
				case 'dnsomatic':
400
					/* Example syntax 
401
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
402
					*/
403
					$needsIP = FALSE;
404
					log_error("DNS-O-Matic: DNS update() starting.");
405
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
406
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
407
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
408
					$server = "https://" . $this->_dnsUser . ":" . $this->_dnsPass . "@updates.dnsomatic.com/nic/update?hostname=";
409
					if($this->_dnsServer)
410
						$server = $this->_dnsServer;
411
					if($this->_dnsPort)
412
						$port = ":" . $this->_dnsPort;
413
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
414
					$data = curl_exec($ch);
415
					if (@curl_error($ch)) log_error("Request completed. DNS-O-Matic reported: " . curl_error($ch));
416
					curl_close($ch);
417
					$this->_checkStatus($data);
418
					break;
419
				default:
420
					break;
421
			}
422
		}
423

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

    
739
		/*
740
		 * Private Function (added 12 July 05) [beta]
741
		 *   Return Error, Set Last Error, and Die.
742
		 */
743
		function _error($errorNumber = '1') {
744
			switch ($errorNumber) {
745
				case 0:
746
					break;
747
				case 2:
748
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
749
					break;
750
				case 3:
751
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
752
					break;
753
				case 4:
754
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
755
					break;
756
				case 5:
757
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
758
					break;
759
				case 6:
760
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
761
					break;
762
				case 7:
763
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
764
					break;
765
				case 10:
766
					$error = 'phpDynDNS: No Change In My IP Address and/or 25 Days Has Not Past. Not Updating Dynamic DNS Entry.';
767
					break;
768
				default:
769
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
770
					/* FIXME: $data isn't in scope here */
771
					/* $this->_debug($data); */
772
					break;
773
			}
774
			$this->lastError = $error;
775
			log_error($error);
776
		}
777

    
778
		/*
779
		 * Private Function (added 12 July 05) [beta]
780
		 *   - Detect whether or not IP needs to be updated.
781
		 *      | Written Specifically for pfSense (pfsense.com) may
782
		 *      | work with other systems. pfSense base is FreeBSD.
783
		 */
784
		function _detectChange() {
785
			
786
			log_error("DynDns: _detectChange() starting.");
787
		
788
			$currentTime = time();
789

    
790
			$wan_ip = $this->_checkIP();
791
			$this->_dnsIP = $wan_ip;
792
			log_error("DynDns: Current WAN IP: {$wan_ip}");
793

    
794
			if (file_exists($this->_cacheFile)) {
795
				if(file_exists($this->_cacheFile))
796
					$contents = file_get_contents($this->_cacheFile);
797
				else
798
					$contents = "";
799
				list($cacheIP,$cacheTime) = split(':', $contents);
800
				$this->_debug($cacheIP.'/'.$cacheTime);
801
				$initial = false;
802
				log_error("DynDns: Cached IP: {$cacheIP}");
803
			} else {
804
				conf_mount_rw();
805
				$file = fopen($this->_cacheFile, 'w');
806
				fwrite($file, '0.0.0.0:'.$currentTime);
807
				fclose($file);
808
				conf_mount_ro();
809
				$cacheIP = '0.0.0.0';
810
				$cacheTime = $currentTime;
811
				$initial = true;
812
				log_error("DynDns: No Cached IP found.");
813
			}
814

    
815
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
816
			 *   zoneedit, dyns, ods
817
			 */
818
			$time = '2160000';
819

    
820
			$needs_updating = FALSE;
821
			/* lets determine if the item needs updating */
822
			if ($cacheIP != $wan_ip) {
823
				$needs_updating = true;
824
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
825
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
826
			}
827
			if (($currentTime - $cacheTime) > $time ) {
828
				$needs_updating = true;
829
				$update_reason = "DynDns: More than 25 days.  Updating. ";
830
				$update_reason .= "{$currentTime} - {$cacheTime} > {$time} ";
831
			}
832
			if ($initial == true) {
833
				$needs_updating = true;
834
				$update_reason .= "Inital update. ";
835
			}
836

    
837
			/*   finally if we need updating then store the
838
			 *   new cache value and return true
839
                         */
840
			if ($needs_updating == true) {
841
				log_error("DynDns debug information: {$update_reason}");
842
				return true;
843
			}
844

    
845
			return false;			
846
		}
847

    
848
		/*
849
		 * Private Funcation (added 16 July 05) [beta]
850
		 *   - Writes debug information to a file.
851
		 *   - This function is only called when a unknown response
852
		 *   - status is returned from a DynDNS service provider.
853
		 */
854
		function _debug ($data) {
855
			$string = '\n'.date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data.'\n';
856
			conf_mount_rw();
857
			$file = fopen($this->_debugFile, 'a');
858
			fwrite($file, $string);
859
			fclose($file);
860
			conf_mount_ro();
861
		}
862
		function _checkIP() {
863

    
864
			log_error("DynDns: _checkIP() starting.");
865

    
866
			$ip_address = find_interface_ip($this->_if);
867
			$this->_ifIP = $ip_address;
868
			if (is_private_ip($ip_address)) {
869
				$hosttocheck = "checkip.dyndns.org";
870
				$checkip = gethostbyname($hosttocheck);
871
				$ip_ch = curl_init("http://{$checkip}");
872
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
873
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
874
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
875
				$ip_result_page = curl_exec($ip_ch);
876
				curl_close($ip_ch);
877
				$ip_result_decoded = urldecode($ip_result_page);
878
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
879
				$ip_address = trim($matches[1]);
880
				log_error("DynDns debug information: {$ip_address} extracted from {$hosttocheck}");
881
			} else
882
				log_error("DynDns debug information: {$ip_address} extracted from local system.");
883

    
884
			return $ip_address;
885
		}
886

    
887
	}
888

    
889
?>
(13-13/50)