Project

General

Profile

Download (34.9 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
		var $_dnsWildcard;
72
		var $_dnsMX;
73
		var $_dnsBackMX;
74
		var $_dnsWanip;
75
		var $_dnsServer;
76
		var $_dnsPort;
77
		var $_dnsUpdateURL;
78
		var $status;
79
		var $_debugID;
80
		var $_if;
81
		
82
		/* 
83
		 * Public Constructor Function (added 12 July 05) [beta]
84
		 *   - Gets the dice rolling for the update. 
85
		 */
86
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '',
87
				    $dnsWildcard = 'OFF', $dnsMX = '', $dnsIf = '', $dnsBackMX = '', $dnsWanip = '',
88
				    $dnsServer = '', $dnsPort = '', $dnsUpdateURL = '') {
89
			
90
			global $config;
91
			
92
			$this->_cacheFile = "/cf/conf/dyndns_{$dnsIf}{$dnsService}.cache";
93
			$this->_debugFile = "/var/etc/dyndns_{$dnsIf}{$dnsService}.debug";
94

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

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

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

    
107
				/* freedns needs this */
108

    
109
				if (!$dnsHost) $this->_error(5);
110
			}
111
			
112
			$this->_dnsService = strtolower($dnsService);
113
			$this->_dnsUser = $dnsUser;
114
			$this->_dnsPass = $dnsPass;
115
			$this->_dnsHost = $dnsHost;
116
			$this->_dnsWanip = $dnsWanip;
117
			$this->_dnsServer = $dnsServer;
118
			$this->_dnsPort = $dnsPort;
119
			$this->_dnsWildcard = $dnsWildcard;
120
			$this->_dnsMX = $dnsMX;
121
			$this->_if = get_real_interface($dnsIf);
122
			
123
			$this->_dnsIP = get_interface_ip($dnsIf);
124
			$this->_debugID = rand(1000000, 9999999);
125
			
126
			if ($this->_detectChange() == FALSE) {
127
				$this->_error(10);
128
			} else {
129
				if ($this->_dnsService == 'dnsomatic' ||
130
					$this->_dnsService == 'dyndns' ||
131
					$this->_dnsService == 'dyndns-static' ||
132
					$this->_dnsService == 'dyndns-custom' ||
133
					$this->_dnsService == 'dhs' ||
134
					$this->_dnsService == 'noip' ||
135
					$this->_dnsService == 'easydns' ||
136
					$this->_dnsService == 'hn' ||
137
					$this->_dnsService == 'zoneedit' ||
138
					$this->_dnsService == 'dyns' ||
139
					$this->_dnsService == 'ods' ||
140
					$this->_dnsService == 'freedns' ||
141
					$this->_dnsService == 'loopia' ||
142
					$this->_dnsService == 'staticcling'||
143
					$this->_dnsService == 'dnsexit' ||
144
					$this->_dnsService == 'opendns')
145
				{
146
					$this->_update();
147
				} else {
148
					$this->_error(6);
149
				}
150
			}					
151
		}
152
			
153
		/*
154
		 * Private Function (added 12 July 05) [beta]
155
		 *   Send Update To Selected Service.
156
		 */
157
		function _update() {
158
		
159
			log_error("DynDns: DynDns _update() starting.");
160
		
161
			if ($this->_dnsService != 'ods') {
162
				$ch = curl_init();
163
				curl_setopt($ch, CURLOPT_HEADER, 0);
164
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
165
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
166
			}
167

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

    
305
					$server = "https://dynamic.zoneedit.com/auth/dynamic.html";
306
					$port = "";
307
					if($this->_dnsServer)
308
						$server = $this->_dnsServer;
309
					if($this->_dnsPort)
310
						$port = ":" . $this->_dnsPort;
311
					curl_setopt($ch, CURLOPT_URL, "{$server}{$port}?host=" .$this->_dnsHost);
312

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

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

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

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

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

    
816
			$wan_ip = $this->_checkIP();
817
			$this->_dnsIP = $wan_ip;
818
			log_error("DynDns: Current WAN IP: {$wan_ip}");
819

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

    
841
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
842
			 *   zoneedit, dyns, ods
843
			 */
844
			$time = '2160000';
845

    
846
			$needs_updating = FALSE;
847
			/* lets deterimine if the item needs updating */
848
			if ($cacheIP != $wan_ip) {
849
				$needs_updating = TRUE;
850
				log_error("DynDns: cacheIP != wan_ip.  Updating.");
851
			}
852
			$update_reason = "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
853
			if (($currentTime - $cacheTime) > $time ) {
854
				$needs_updating = TRUE;
855
				log_error("DynDns: More than 25 days.  Updating.");
856
			}
857
			$update_reason .= "{$currentTime} - {$cacheTime} > {$time} ";
858
			if ($initial == TRUE) {
859
				$needs_updating = TRUE;
860
				$update_reason .= "Inital update. ";
861
				log_error("DynDns: Initial run.   Updating.");
862
			}
863
			/*   finally if we need updating then store the
864
			 *   new cache value and return true
865
                         */
866
			if($needs_updating == TRUE) {
867
				return TRUE;
868
			} else {
869
				return FALSE;			
870
			}
871
			
872
			log_error("DynDns debug information: {$update_reason}");
873
			
874
		}
875

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

    
892
			log_error("DynDns: _checkIP() starting.");
893

    
894
			$ip_ch = curl_init('http://checkip.dyndns.org');
895
			curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
896
			curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
897
			curl_setopt($ip_ch, CURLOPT_INTERFACE, $this->_if);
898
			$ip_result_page = curl_exec($ip_ch);
899
			curl_close($ip_ch);
900
			$ip_result_decoded = urldecode($ip_result_page);
901
			preg_match('=Current IP Address: (.*)</body>=siU', $ip_result_decoded, $matches);
902
			$ip_address = trim($matches[1]);
903

    
904
			log_error("DynDns debug information: {$ip_address}");
905

    
906
			return $ip_address;
907
		}
908

    
909
	}
910

    
911
?>
(11-11/42)