Project

General

Profile

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

    
55
	class updatedns {
56
		var $_cacheFile = '/cf/conf/dyndns.cache';
57
		var $_debugFile = '/var/etc/dyndns.debug';
58
		var $_UserAgent = 'User-Agent: phpDynDNS/0.7';
59
		var $_errorVerbosity = 0;
60
		var $_dnsService;
61
		var $_dnsUser;
62
		var $_dnsPass;
63
		var $_dnsHost;
64
		var $_dnsIP;
65
		var $_dnsWildcard;
66
		var $_dnsMX;
67
		var $_dnsBackMX;
68
		var $_dnsWanip;
69
		var $_dnsServer;
70
		var $_dnsPort;
71
		var $_dnsUpdateURL;
72
		var $status;
73
		var $_debugID;
74
		
75
		/* 
76
		 * Public Constructor Function (added 12 July 05) [beta]
77
		 *   - Gets the dice rolling for the update. 
78
		 */
79
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '',
80
				    $dnsWildcard = 'OFF', $dnsMX = '', $dnsBackMX = '', $dnsWanip = '',
81
				    $dnsServer = '', $dnsPort = '', $dnsUpdateURL = '') {
82
			
83
			global $config;
84
			
85
			log_error("DynDns: updatedns() starting");
86
			
87
			if (!$dnsService) $this->_error(2);
88
			if (!($dnsService == 'freedns')) {
89

    
90
				/* all services except freedns use these */
91

    
92
				if (!$dnsUser) $this->_error(3);
93
				if (!$dnsPass) $this->_error(4);
94
				if (!$dnsHost) $this->_error(5);
95
			} else {
96

    
97
				/* freedns needs this */
98

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

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

    
295
					$server = "https://dynamic.zoneedit.com/auth/dynamic.html";
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}?host=" .$this->_dnsHost);
302

    
303
					$data = curl_exec($ch);
304
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
305
					curl_close($ch);
306
					$this->_checkStatus($data);
307
					break;
308
				case 'dyns':
309
					$needsIP = FALSE;
310
					$server = "http://www.dyns.cx/postscript011.php";
311
					$port = "";
312
					if($this->_dnsServer)
313
						$server = $this->_dnsServer;
314
					if($this->_dnsPort)
315
						$port = ":" . $this->_dnsPort;					
316
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . $this->_dnsUser . '&password=' . $this->_dnsPass . '&host=' . $this->_dnsHost);
317
					$data = curl_exec($ch);
318
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
319
					curl_close($ch);
320
					$this->_checkStatus($data);
321
					break;
322
				case 'ods':
323
					$needsIP = FALSE;
324
					$misc_errno = 0;
325
					$misc_error = "";
326
					$server = "ods.org";
327
					$port = "";
328
					if($this->_dnsServer)
329
						$server = $this->_dnsServer;
330
					if($this->_dnsPort)
331
						$port = ":" . $this->_dnsPort;						
332
					$this->con['socket'] = fsockopen("{$server}{$port}", "7070", $misc_errno, $misc_error, 30);
333
					/* Check that we have connected */
334
					if (!$this->con['socket']) {
335
						print "error! could not connect.";
336
						break;
337
					}
338
					/* Here is the loop. Read the incoming data (from the socket connection) */
339
					while (!feof($this->con['socket'])) {
340
						$this->con['buffer']['all'] = trim(fgets($this->con['socket'], 4096));
341
						$code = substr($this->con['buffer']['all'], 0, 3);
342
						sleep(1);
343
						switch($code) {
344
							case 100:
345
								fputs($this->con['socket'], "LOGIN ".$this->_dnsUser." ".$this->_dnsPass."\n");
346
								break;
347
							case 225:
348
								fputs($this->con['socket'], "DELRR ".$this->_dnsHost." A\n");
349
								break;
350
							case 901:
351
								fputs($this->con['socket'], "ADDRR ".$this->_dnsHost." A ".$this->_dnsIP."\n");
352
								break;
353
							case 795:
354
								fputs($this->con['socket'], "QUIT\n");
355
								break;
356
						}
357
					}
358
					$this->_checkStatus($code);
359
					break;
360
				case 'freedns':
361
					$needIP = FALSE;
362
					curl_setopt($ch, CURLOPT_URL, 'http://freedns.afraid.org/dynamic/update.php?' . $this->_dnsHost);
363
					$data = curl_exec($ch);
364
					if (@curl_error($ch)) log_error("Curl errror occured: " . 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 errror occured: " . curl_error($ch));
375
					curl_close($ch);
376
					$this->_checkStatus($data);
377
					break;
378
	            case 'staticcling':
379
                    $needsIP = FALSE;
380
                    curl_setopt($ch, CURLOPT_URL, 'http://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
381
                    $data = curl_exec($ch);
382
                    if (@curl_error($ch)) log_error("Curl error occured: " . curl_error($ch));
383
                    curl_close($ch);
384
                    $this->_checkStatus($data);
385
                    break;	                    
386
				case 'dnsomatic':
387
				
388
					/* Example syntax 
389
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
390
					*/
391
					
392
					$needsIP = FALSE;
393
					log_error("DNS-O-Matic: DNS update() starting.");
394
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
395
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
396
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
397
					$server = "https://" . $this->_dnsUser . ":" . $this->_dnsPass . "@updates.dnsomatic.com/nic/update?hostname=";
398
					if($this->_dnsServer)
399
						$server = $this->_dnsServer;
400
					if($this->_dnsPort)
401
						$port = ":" . $this->_dnsPort;
402
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
403
					$data = curl_exec($ch);
404
					if (@curl_error($ch)) log_error("Request completed. DNS-O-Matic reported: " . curl_error($ch));
405
					curl_close($ch);
406
					$this->_checkStatus($data);
407
					break;
408
				default:
409
					break;
410
			}
411
		}
412

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

    
696
		/*
697
		 * Private Function (added 12 July 05) [beta]
698
		 *   Return Error, Set Last Error, and Die.
699
		 */
700
		function _error($errorNumber = '1') {
701
			switch ($errorNumber) {
702
				case 0:
703
					break;
704
				case 2:
705
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
706
					break;
707
				case 3:
708
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
709
					break;
710
				case 4:
711
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
712
					break;
713
				case 5:
714
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
715
					break;
716
				case 6:
717
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
718
					break;
719
				case 7:
720
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
721
					break;
722
				case 10:
723
					$error = 'phpDynDNS: No Change In My IP Address and/or 25 Days Has Not Past. Not Updating Dynamic DNS Entry.';
724
					break;
725
				default:
726
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
727
					/* FIXME: $data isn't in scope here */
728
					/* $this->_debug($data); */
729
					break;
730
			}
731
			$this->lastError = $error;
732
			log_error($error);
733
		}
734

    
735
		/*
736
		 * Private Function (added 12 July 05) [beta]
737
		 *   - Detect whether or not IP needs to be updated.
738
		 *      | Written Specifically for pfSense (pfsense.com) may
739
		 *      | work with other systems. pfSense base is FreeBSD.
740
		 */
741
		function _detectChange() {
742
			
743
			log_error("DynDns: _detectChange() starting.");
744
		
745
			$currentTime = time();
746

    
747
			$wan_ip = $this->_checkIP();
748
			$this->_dnsIP = $wan_ip;
749
			log_error("DynDns: Current WAN IP: {$wan_ip}");
750

    
751
			if (file_exists($this->_cacheFile)) {
752
				if(file_exists($this->_cacheFile))
753
					$contents = file_get_contents($this->_cacheFile);
754
				else
755
					$contents = "";
756
				list($cacheIP,$cacheTime) = split(':', $contents);
757
				$this->_debug($cacheIP.'/'.$cacheTime);
758
				$initial = false;
759
				log_error("DynDns: Cached IP: {$cacheIP}");
760
			} else {
761
				conf_mount_rw();
762
				$file = fopen($this->_cacheFile, 'w');
763
				fwrite($file, '0.0.0.0:'.$currentTime);
764
				fclose($file);
765
				conf_mount_ro();
766
				$cacheIP = '0.0.0.0';
767
				$cacheTime = $currentTime;
768
				$initial = true;
769
				log_error("DynDns: No Cached IP found.");
770
			}
771

    
772
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
773
			 *   zoneedit, dyns, ods
774
			 */
775
			$time = '2160000';
776

    
777
			$needs_updating = FALSE;
778
			/* lets deterimine if the item needs updating */
779
			if ($cacheIP != $wan_ip) {
780
				$needs_updating = TRUE;
781
				log_error("DynDns: cacheIP != wan_ip.  Updating.");
782
			}
783
			$update_reason = "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
784
			if (($currentTime - $cacheTime) > $time ) {
785
				$needs_updating = TRUE;
786
				log_error("DynDns: More than 25 days.  Updating.");
787
			}
788
			$update_reason .= "{$currentTime} - {$cacheTime} > {$time} ";
789
			if ($initial == TRUE) {
790
				$needs_updating = TRUE;
791
				$update_reason .= "Inital update. ";
792
				log_error("DynDns: Initial run.   Updating.");
793
			}
794
			/*   finally if we need updating then store the
795
			 *   new cache value and return true
796
                         */
797
			if($needs_updating == TRUE) {
798
				return TRUE;
799
			} else {
800
				return FALSE;			
801
			}
802
			
803
			log_error("DynDns debug information: {$update_reason}");
804
			
805
		}
806

    
807
		/*
808
		 * Private Funcation (added 16 July 05) [beta]
809
		 *   - Writes debug information to a file.
810
		 *   - This function is only called when a unknown response
811
		 *   - status is returned from a DynDNS service provider.
812
		 */
813
		function _debug ($data) {
814
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data.'\n';
815
			conf_mount_rw();
816
			$file = fopen($this->_debugFile, 'a');
817
			fwrite($file, $string);
818
			fclose($file);
819
			conf_mount_ro();
820
		}
821
		function _checkIP() {
822

    
823
			log_error("DynDns: _checkIP() starting.");
824

    
825
			$ip_ch = curl_init('http://checkip.dyndns.org');
826
			curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
827
			curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
828
			$ip_result_page = curl_exec($ip_ch);
829
			$ip_result_decoded = urldecode($ip_result_page);
830
			preg_match('=Current IP Address: (.*)</body>=siU', $ip_result_decoded, $matches);
831
			$ip_address = trim($matches[1]);
832

    
833
			log_error("DynDns debug information: {$ip_address}");
834

    
835
			return $ip_address;
836
		}
837

    
838
	}
839

    
840
?>
(7-7/29)