Project

General

Profile

Download (33.8 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
					/* Example syntax 
425
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
426
					*/
427
					$needsIP = FALSE;
428
					log_error("DNS-O-Matic: DNS update() starting.");
429
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
430
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
431
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
432
					$server = "https://" . $this->_dnsUser . ":" . $this->_dnsPass . "@updates.dnsomatic.com/nic/update?hostname=";
433
					if($this->_dnsServer)
434
						$server = $this->_dnsServer;
435
					if($this->_dnsPort)
436
						$port = ":" . $this->_dnsPort;
437
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
438
					$data = curl_exec($ch);
439
					if (@curl_error($ch)) log_error("Request completed. DNS-O-Matic reported: " . curl_error($ch));
440
					curl_close($ch);
441
					$this->_checkStatus($data);
442
					break;
443
				default:
444
					break;
445
			}
446
		}
447

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

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

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

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

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

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

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

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

    
890
			log_error("DynDns: _checkIP() starting.");
891

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

    
902
			log_error("DynDns debug information: {$ip_address}");
903

    
904
			return $ip_address;
905
		}
906

    
907
	}
908

    
909
?>
(13-13/50)