Project

General

Profile

Download (34.1 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
			}
175

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

    
313
					$server = "https://dynamic.zoneedit.com/auth/dynamic.html";
314
					$port = "";
315
					if($this->_dnsServer)
316
						$server = $this->_dnsServer;
317
					if($this->_dnsPort)
318
						$port = ":" . $this->_dnsPort;
319
					curl_setopt($ch, CURLOPT_URL, "{$server}{$port}?host=" .$this->_dnsHost);
320

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

    
423
				case 'staticcling':
424
					$needsIP = FALSE;
425
					curl_setopt($ch, CURLOPT_URL, 'http://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
426
					$data = curl_exec($ch);
427
					if (@curl_error($ch)) log_error("Curl error occured: " . curl_error($ch));
428
					curl_close($ch);
429
					$this->_checkStatus($data);
430
					break;	                    
431
				case 'dnsomatic':
432
					/* Example syntax 
433
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
434
					*/
435
					$needsIP = FALSE;
436
					log_error("DNS-O-Matic: DNS update() starting.");
437
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
438
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
439
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
440
					$server = "https://" . $this->_dnsUser . ":" . $this->_dnsPass . "@updates.dnsomatic.com/nic/update?hostname=";
441
					if($this->_dnsServer)
442
						$server = $this->_dnsServer;
443
					if($this->_dnsPort)
444
						$port = ":" . $this->_dnsPort;
445
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
446
					$data = curl_exec($ch);
447
					if (@curl_error($ch)) log_error("Request completed. DNS-O-Matic reported: " . curl_error($ch));
448
					curl_close($ch);
449
					$this->_checkStatus($data);
450
					break;
451
				default:
452
					break;
453
			}
454
		}
455

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

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

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

    
822
			$wan_ip = $this->_checkIP();
823
			$this->_dnsIP = $wan_ip;
824
			log_error("DynDns: Current WAN IP: {$wan_ip}");
825

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

    
847
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
848
			 *   zoneedit, dyns, ods
849
			 */
850
			$time = '2160000';
851

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

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

    
877
			return false;			
878
		}
879

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

    
896
			log_error("DynDns: _checkIP() starting.");
897

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

    
915
			return $ip_address;
916
		}
917

    
918
	}
919

    
920
?>
(13-13/52)