Project

General

Profile

Feature #1258 » dyndns.class.txt

Chris Goundry, 02/06/2011 02:23 PM

 
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 'dnsmadeeasy':
152
				case 'staticcling':
153
				case 'dnsexit':
154
				case 'opendns':
155
				case 'namecheap':
156
					$this->_update();	
157
					break;
158
				default:
159
					$this->_error(6);
160
					break;
161
				}
162
			}
163
		}
164
			
165
		/*
166
		 * Private Function (added 12 July 05) [beta]
167
		 *   Send Update To Selected Service.
168
		 */
169
		function _update() {
170
		
171
			log_error("DynDns: DynDns _update() starting.");
172
		
173
			if ($this->_dnsService != 'ods') {
174
				$ch = curl_init();
175
				curl_setopt($ch, CURLOPT_HEADER, 0);
176
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
177
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
178
				curl_setopt($ch, CURLOPT_INTERFACE, $this->_ifIP);
179
				curl_setopt($ch, CURLOPT_TIMEOUT, 60); // Completely empirical
180
			}
181

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

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

    
295
					$data = curl_exec($ch);
296
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
297
					curl_close($ch);
298
					$this->_checkStatus($data);
299
					break;
300
				case 'dyns':
301
					$needsIP = FALSE;
302
					$server = "http://www.dyns.cx/postscript011.php";
303
					$port = "";
304
					if($this->_dnsServer)
305
						$server = $this->_dnsServer;
306
					if($this->_dnsPort)
307
						$port = ":" . $this->_dnsPort;					
308
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&password=' . $this->_dnsPass . '&host=' . $this->_dnsHost);
309
					$data = curl_exec($ch);
310
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
311
					curl_close($ch);
312
					$this->_checkStatus($data);
313
					break;
314
				case 'ods':
315
					$needsIP = FALSE;
316
					$misc_errno = 0;
317
					$misc_error = "";
318
					$server = "ods.org";
319
					$port = "";
320
					if($this->_dnsServer)
321
						$server = $this->_dnsServer;
322
					if($this->_dnsPort)
323
						$port = ":" . $this->_dnsPort;						
324
					$this->con['socket'] = fsockopen("{$server}{$port}", "7070", $misc_errno, $misc_error, 30);
325
					/* Check that we have connected */
326
					if (!$this->con['socket']) {
327
						print "error! could not connect.";
328
						break;
329
					}
330
					/* Here is the loop. Read the incoming data (from the socket connection) */
331
					while (!feof($this->con['socket'])) {
332
						$this->con['buffer']['all'] = trim(fgets($this->con['socket'], 4096));
333
						$code = substr($this->con['buffer']['all'], 0, 3);
334
						sleep(1);
335
						switch($code) {
336
							case 100:
337
								fputs($this->con['socket'], "LOGIN ".$this->_dnsUser." ".$this->_dnsPass."\n");
338
								break;
339
							case 225:
340
								fputs($this->con['socket'], "DELRR ".$this->_dnsHost." A\n");
341
								break;
342
							case 901:
343
								fputs($this->con['socket'], "ADDRR ".$this->_dnsHost." A ".$this->_dnsIP."\n");
344
								break;
345
							case 795:
346
								fputs($this->con['socket'], "QUIT\n");
347
								break;
348
						}
349
					}
350
					$this->_checkStatus($code);
351
					break;
352
				case 'freedns':
353
					$needIP = FALSE;
354
					curl_setopt($ch, CURLOPT_URL, 'http://freedns.afraid.org/dynamic/update.php?' . $this->_dnsHost);
355
					$data = curl_exec($ch);
356
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
357
					curl_close($ch);
358
					$this->_checkStatus($data);
359
					break;
360
				case 'dnsexit':
361
					$needsIP = TRUE;
362
					curl_setopt($ch, CURLOPT_URL, 'http://www.dnsexit.com/RemoteUpdate.sv?login='.$this->_dnsUser. '&password='.$this->_dnsPass.'&host='.$this->_dnsHost.'&myip='.$this->_dnsIP);
363
					$data = curl_exec($ch);
364
					if (@curl_error($ch)) log_error("Curl error occurred:" . curl_error($ch));
365
					curl_close($ch);
366
					$this->_checkStatus($data);
367
					break;
368
				case 'loopia':
369
					$needsIP = TRUE;
370
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
371
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
372
					curl_setopt($ch, CURLOPT_URL, 'https://dns.loopia.se/XDynDNSServer/XDynDNS.php?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP);
373
					$data = curl_exec($ch);
374
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
375
					curl_close($ch);
376
					$this->_checkStatus($data);
377
					break;
378
				case 'dnsmadeeasy':
379
					$needsIP = TRUE;
380
					$server = "https://www.dnsmadeeasy.com/servlet/updateip?"; 
381
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
382
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
383
					curl_setopt($ch, CURLOPT_URL, $server.'username='.$this->_dnsUser.'&password='.$this->_dnsPass.'&id='.$this->_dnsHost.'&ip='.$this->_dnsIP);
384
					$data = curl_exec($ch);
385
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
386
					curl_close($ch);
387
					$this->_checkStatus($data);
388
					break;
389
				case 'opendns':
390
					$needsIP = FALSE;
391
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
392
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
393
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
394
					$server = "https://updates.opendns.com/nic/update?hostname=". $this->_dnsHost;
395
					$port = "";
396
					if($this->_dnsServer)
397
						$server = $this->_dnsServer;
398
					if($this->_dnsPort)
399
						$port = ":" . $this->_dnsPort;
400
					curl_setopt($ch, CURLOPT_URL, $server .$port);
401
					$data = curl_exec($ch);
402
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
403
					curl_close($ch);
404
					$this->_checkStatus($data);
405
					break;
406
				case 'staticcling':
407
					$needsIP = FALSE;
408
					curl_setopt($ch, CURLOPT_URL, 'http://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
409
					$data = curl_exec($ch);
410
					if (@curl_error($ch)) log_error("Curl error occured: " . curl_error($ch));
411
					curl_close($ch);
412
					$this->_checkStatus($data);
413
					break;	                    
414
				case 'dnsomatic':
415
					/* Example syntax 
416
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
417
					*/
418
					$needsIP = FALSE;
419
					log_error("DNS-O-Matic: DNS update() starting.");
420
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
421
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
422
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
423
					$server = "https://" . $this->_dnsUser . ":" . $this->_dnsPass . "@updates.dnsomatic.com/nic/update?hostname=";
424
					if($this->_dnsServer)
425
						$server = $this->_dnsServer;
426
					if($this->_dnsPort)
427
						$port = ":" . $this->_dnsPort;
428
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
429
					$data = curl_exec($ch);
430
					if (@curl_error($ch)) log_error("Request completed. DNS-O-Matic reported: " . curl_error($ch));
431
					curl_close($ch);
432
					$this->_checkStatus($data);
433
					break;
434
				case 'namecheap':
435
					/* Example:
436
						https://dynamicdns.park-your-domain.com/update?host=[host_name]&domain=[domain.com]&password=[domain_password]&ip=[your_ip]
437
					*/
438
					$needsIP = FALSE;
439
					log_error("Namecheap: DNS update() starting.");
440
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
441
					list($hostname, $domain) = explode(".", $this->_dnsHost, 2);
442
					$server = "https://dynamicdns.park-your-domain.com/update?host={$hostname}&domain={$domain}&password={$this->_dnsPass}&ip={$this->_dnsIP}";
443
					curl_setopt($ch, CURLOPT_URL, $server);
444
					$data = curl_exec($ch);
445
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
446
					curl_close($ch);
447
					$this->_checkStatus($data);
448
				default:
449
					break;
450
			}
451
		}
452

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

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

    
841
		/*
842
		 * Private Function (added 12 July 05) [beta]
843
		 *   - Detect whether or not IP needs to be updated.
844
		 *      | Written Specifically for pfSense (pfsense.com) may
845
		 *      | work with other systems. pfSense base is FreeBSD.
846
		 */
847
		function _detectChange() {
848
			
849
			log_error("DynDns: _detectChange() starting.");
850
		
851
			$currentTime = time();
852

    
853
			$wan_ip = $this->_checkIP();
854
			$this->_dnsIP = $wan_ip;
855
			log_error("DynDns: Current WAN IP: {$wan_ip}");
856

    
857
			if (file_exists($this->_cacheFile)) {
858
				$contents = file_get_contents($this->_cacheFile);
859
				list($cacheIP,$cacheTime) = split(':', $contents);
860
				$this->_debug($cacheIP.'/'.$cacheTime);
861
				$initial = false;
862
				log_error("DynDns: Cached IP: {$cacheIP}");
863
			} else {
864
				conf_mount_rw();
865
				$file = fopen($this->_cacheFile, 'w');
866
				fwrite($file, '0.0.0.0:'.$currentTime);
867
				fclose($file);
868
				conf_mount_ro();
869
				$cacheIP = '0.0.0.0';
870
				$cacheTime = $currentTime;
871
				$initial = true;
872
				log_error("DynDns: No Cached IP found.");
873
			}
874

    
875
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
876
			 *   zoneedit, dyns, ods
877
			 */
878
			$time = '2160000';
879

    
880
			$needs_updating = FALSE;
881
			/* lets determine if the item needs updating */
882
			if ($cacheIP != $wan_ip) {
883
				$needs_updating = true;
884
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
885
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
886
			}
887
			if (($currentTime - $cacheTime) > $time ) {
888
				$needs_updating = true;
889
				$update_reason = "DynDns: More than 25 days.  Updating. ";
890
				$update_reason .= "{$currentTime} - {$cacheTime} > {$time} ";
891
			}
892
			if ($initial == true) {
893
				$needs_updating = true;
894
				$update_reason .= "Inital update. ";
895
			}
896

    
897
			/*   finally if we need updating then store the
898
			 *   new cache value and return true
899
                         */
900
			if ($needs_updating == true) {
901
				log_error("DynDns debug information: {$update_reason}");
902
				return true;
903
			}
904

    
905
			return false;			
906
		}
907

    
908
		/*
909
		 * Private Funcation (added 16 July 05) [beta]
910
		 *   - Writes debug information to a file.
911
		 *   - This function is only called when a unknown response
912
		 *   - status is returned from a DynDNS service provider.
913
		 */
914
		function _debug ($data) {
915
			$string = '\n'.date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data.'\n';
916
			conf_mount_rw();
917
			$file = fopen($this->_debugFile, 'a');
918
			fwrite($file, $string);
919
			fclose($file);
920
			conf_mount_ro();
921
		}
922
		function _checkIP() {
923

    
924
			log_error("DynDns: _checkIP() starting.");
925

    
926
			$ip_address = find_interface_ip($this->_if);
927
			$this->_ifIP = $ip_address;
928
			if (is_private_ip($ip_address)) {
929
				$hosttocheck = "checkip.dyndns.org";
930
				$try = 0;
931
				while ($try < 3) {
932
					$checkip = gethostbyname($hosttocheck);
933
					if (is_ipaddr($checkip))
934
						break;
935
					$try++;
936
				}
937
				if ($try >= 3) {
938
					log_error("Dyndns debug information: Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
939
					return $ip_address; /* XXX: Might return private ip address! */
940
				}
941
				$ip_ch = curl_init("http://{$checkip}");
942
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
943
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
944
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
945
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
946
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 60);
947
				$ip_result_page = curl_exec($ip_ch);
948
				curl_close($ip_ch);
949
				$ip_result_decoded = urldecode($ip_result_page);
950
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
951
				$ip_address = trim($matches[1]);
952
				log_error("DynDns debug information: {$ip_address} extracted from {$hosttocheck}");
953
			} else
954
				log_error("DynDns debug information: {$ip_address} extracted from local system.");
955

    
956
			return $ip_address;
957
		}
958

    
959
	}
960

    
961
?>
(1-1/3)