Project

General

Profile

Download (16.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?
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)
13
	 *    - DynS (dyns.org)
14
	 *    - ZoneEdit (zoneedit.com)
15
	 * +----------------------------------------------------+
16
	 *  Requirements:
17
	 *    - PHP version 4.0.2 or higher with CURL Library
18
	 * +----------------------------------------------------+
19
	 *  Public Functions
20
	 *    - updatedns()
21
	 *
22
	 *  Private Functions
23
	 *    - _update()
24
	 *    - _checkStatus()
25
	 *    - _error()
26
	 *    - _detectChange()
27
	 *    - _debug()
28
	 * +----------------------------------------------------+
29
	 *  DynDNS Dynamic - Last Tested: 12 July 2005
30
	 *  DynDNS Static  - Last Tested: NEVER
31
	 *  DynDNS Custom  - Last Tested: NEVER
32
	 *  No-IP          - Last Tested: 12 July 2005
33
	 *  HN.org         - Last Tested: 12 July 2005
34
	 *  EasyDNS        - Last Tested: NEVER
35
	 *  DHS            - Last Tested: 12 July 2005
36
	 *  ZoneEdit       - Last Tested: NEVER
37
	 *  Dyns           - Last Tested: NEVER
38
	 * +====================================================+
39
	 *
40
	 * @author 		E.Kristensen
41
	 * @link    	http://www.idylldesigns.com/projects/phpdns/
42
	 * @version 	0.6
43
	 * @updated		17 July 05 at 16:49:23 GMT
44
	 *
45
	 */
46

    
47
	class updatedns {
48
		var $_cacheFile = '/var/etc/dyndns.cache';
49
		var $_debugFile = '/var/etc/dyndns.debug';
50
		var $_UserAgent = 'User-Agent: phpDynDNS/0.4';
51
		var $_errorVerbosity = 0;
52
		var $_dnsService;
53
		var $_dnsUser;
54
		var $_dnsPass;
55
		var $_dnsHost;
56
		var $_dnsIP;
57
		var $_dnsWildcard;
58
		var $_dnsMX;
59
		var $_dnsBackMX;
60
		var $status;
61

    
62
		/* 
63
		 * Public Constructor Function (added 12 July 05) [beta]
64
		 *   - Gets the dice rolling for the update. 
65
		 */
66
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '', $dnsWildcard = 'OFF', $dnsMX = '', $dnsBackMX = '') {
67
			if (!$dnsService) $this->_error(2);
68
			if (!$dnsUser) $this->_error(3);
69
			if (!$dnsPass) $this->_error(4);
70
			if (!$dnsHost) $this->_error(5);
71

    
72
			$this->_dnsService = strtolower($dnsService);
73
			$this->_dnsUser = $dnsUser;
74
			$this->_dnsPass = $dnsPass;
75
			$this->_dnsHost = $dnsHost;
76
			$this->_dnsIP = $dnsIP;
77

    
78
			if ($this->_detectChange() == FALSE) {
79
				$this->_error(10);
80
			} else {
81
				if ($this->_dnsService == 'dyndns' ||
82
					$this->_dnsService == 'dyndns-static' ||
83
					$this->_dnsService == 'dyndns-custom' ||
84
					$this->_dnsService == 'dhs' ||
85
					$this->_dnsService == 'noip' ||
86
					$this->_dnsService == 'easydns' ||
87
					$this->_dnsService == 'hn' ||
88
					$this->_dnsService == 'zoneedit' ||
89
					$this->_dnsService == 'dyns')
90
				{
91
					$this->_update();
92
				} else {
93
					$this->_error(6);
94
				}
95
			}
96

    
97
		}
98

    
99

    
100
		/*
101
		 * Private Function (added 12 July 05) [beta]
102
		 *   Send Update To Selected Service.
103
		 */
104
		function _update() {
105
			$ch = curl_init();
106
			curl_setopt($ch, CURLOPT_HEADER, 0);
107
			curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
108
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
109

    
110
			switch ($this->_dnsService) {
111
				case 'dyndns':
112
					$needsIP = FALSE;
113
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
114
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
115
					curl_setopt($ch, CURLOPT_URL, 'https://members.dyndns.org/nic/update?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP.'&wildcard='.$this->_dnsWildcard.'&mx='.$this->_dnsMX.'&backmx=NO');
116
					$data = curl_exec($ch);
117
					curl_close($ch);
118
					$this->_checkStatus($data);
119
					break;
120
				case 'dyndns-static':
121
					$needsIP = FALSE;
122
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
123
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
124
					curl_setopt($ch, CURLOPT_URL, 'https://members.dyndns.org/nic/update?system=statdns&hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP.'&wildcard='.$this->_dnsWildcard.'&mx='.$this->_dnsMX.'&backmx=NO');
125
					$data = curl_exec($ch);
126
					curl_close($ch);
127
					$this->_checkStatus($data);
128
					break;
129
				case 'dyndns-custom':
130
					$needsIP = FALSE;
131
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
132
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
133
					curl_setopt($ch, CURLOPT_URL, 'https://members.dyndns.org/nic/update?system=custom&hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP.'&wildcard='.$this->_dnsWildcard.'&mx='.$this->_dnsMX.'&backmx=NO');
134
					$data = curl_exec($ch);
135
					curl_close($ch);
136
					$this->_checkStatus($data);
137
					break;
138
				case 'dhs':
139
					$needsIP = TRUE;
140
						$post_data['hostscmd'] = 'edit';
141
						$post_data['hostscmdstage'] = '2';
142
						$post_data['type'] = '4';
143
						$post_data['updatetype'] = 'Online';
144
						$post_data['mx'] = $this->_dnsMX;
145
						$post_data['mx2'] = '';
146
						$post_data['txt'] = '';
147
						$post_data['offline_url'] = '';
148
						$post_data['cloak'] = 'Y';
149
						$post_data['cloak_title'] = '';
150
						$post_data['ip'] = $this->_dnsIP;
151
						$post_data['domain'] = 'dyn.dhs.org';
152
						$post_data['hostname'] = $this->_dnsHost;
153
						$post_data['submit'] = 'Update';
154
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
155
					curl_setopt($ch, CURLOPT_URL, 'https://members.dhs.org/nic/hosts');
156
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
157
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
158
					$data = curl_exec($ch);
159
					curl_close($ch);
160
					$this->_checkStatus($data);
161
					break;
162
				case 'noip':
163
					$needsIP = TRUE;
164
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
165
					curl_setopt($ch, CURLOPT_URL, 'http://dynupdate.no-ip.com/dns?username='.$this->_dnsUser.'&password='.$this->_dnsPass.'&hostname='.$this->_dnsHost.'&ip='.$this->_dnsIP);
166
					$data = curl_exec($ch);
167
					curl_close($ch);
168
					$this->_checkStatus($data);
169
					break;
170
				case 'easydns':
171
					$needsIP = TRUE;
172
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
173
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
174
					curl_setopt($ch, CURLOPT_URL, 'http://members.easydns.com/dyn/dyndns.php?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP.'&wildcard='.$this->_dnsWildcard.'&mx='.$this->_dnsMX.'&backmx='.$this->_dnsBackMX);
175
					$data = curl_exec($ch);
176
					curl_close($ch);
177
					$this->_checkStatus($data);
178
					break;
179
				case 'hn':
180
					$needsIP = TRUE;
181
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
182
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
183
					curl_setopt($ch, CURLOPT_URL, 'http://dup.hn.org/vanity/update?ver=1&IP='.$this->_dnsIP);
184
					$data = curl_exec($ch);
185
					curl_close($ch);
186
					$this->_checkStatus($data);
187
					break;
188
				case 'zoneedit':
189
					$needsIP = FALSE;
190
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
191
					curl_setopt($ch, CURLOPT_URL, 'https://dynamic.zoneedit.com/auth/dynamic.html?host='.$this->_dnsHost.'&dnsto='.$this->_dnsIP);
192
					$data = curl_exec($ch);
193
					curl_close($ch);
194
					$this->_checkStatus($data);
195
					break;
196
				case 'dyns';
197
					$needsIP = FALSE;
198
					curl_setopt($ch, CURLOPT_URL, 'http://www.dyns.cx/postscript011.php?username='.$this->_dnsUser.'&password='.$this->_dnsPass.'&host='.$this->_dnsHost);
199
					$data = curl_exec($ch);
200
					curl_close($ch);
201
					$this->_checkStatus($data);
202
					break;
203
				default:
204
					break;
205
			}
206
		}
207

    
208

    
209
		/*
210
		 * Private Function (added 12 July 2005) [beta]
211
		 *   Retrieve Update Status
212
		 */
213
		function _checkStatus($data) {
214
			switch ($this->_dnsService) {
215
				case 'dyndns':
216
					if (preg_match('/notfqdn/i', $data)) {
217
						$status = "phpDynDNS: (Error) Not A FQDN!";
218
					} else if (preg_match('/nochg/i', $data)) {
219
						$status = "phpDynDNS: (Success) No Change In IP Address";
220
					} else if (preg_match('/good/i', $data)) {
221
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
222
					} else if (preg_match('/noauth/i', $data)) {
223
						$status = "phpDynDNS: (Error) User Authorization Failed";
224
					} else {
225
						$status = "phpDynDNS: (Unknown Response)";
226
						$this->_debug($data);
227
					}
228
					break;
229
				case 'dyndns-static':
230
					if (preg_match('/notfqdn/i', $data)) {
231
						$status = "phpDynDNS: (Error) Not A FQDN!";
232
					} else if (preg_match('/nochg/i', $data)) {
233
						$status = "phpDynDNS: (Success) No Change In IP Address";
234
					} else if (preg_match('/good/i', $data)) {
235
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
236
					} else if (preg_match('/noauth/i', $data)) {
237
						$status = "phpDynDNS: (Error) User Authorization Failed";
238
					} else {
239
						$status = "phpDynDNS: (Unknown Response)";
240
						$this->_debug($data);
241
					}
242
					break;
243
				case 'dyndns-custom':
244
					if (preg_match('/notfqdn/i', $data)) {
245
						$status = "phpDynDNS: (Error) Not A FQDN!";
246
					} else if (preg_match('/nochg/i', $data)) {
247
						$status = "phpDynDNS: (Success) No Change In IP Address";
248
					} else if (preg_match('/good/i', $data)) {
249
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
250
					} else if (preg_match('/noauth/i', $data)) {
251
						$status = "phpDynDNS: (Error) User Authorization Failed";
252
					} else {
253
						$status = "phpDynDNS: (Unknown Response)";
254
						$this->_debug($data);
255
					}
256
					break;
257
				case 'dhs':
258
					break;
259
				case 'noip':
260
					list($ip,$code) = split(":",$data);
261
					switch ($code) {
262
						case 0:
263
							$status = "phpDynDNS: (Success) IP address is current, no update performed.";
264
							break;
265
						case 1:
266
							$status = "phpDynDNS: (Success) DNS hostname update successful.";
267
							break;
268
						case 2:
269
							$status = "phpDynDNS: (Error) Hostname supplied does not exist.";
270
							break;
271
						case 3:
272
							$status = "phpDynDNS: (Error) Invalid Username.";
273
							break;
274
						case 4:
275
							$status = "phpDynDNS: (Error) Invalid Password.";
276
							break;
277
						case 5:
278
							$status = "phpDynDNS: (Error) To many updates sent.";
279
							break;
280
						case 6:
281
							$status = "phpDynDNS: (Error) Account disabled due to violation of No-IP terms of service.";
282
							break;
283
						case 7:
284
							$status = "phpDynDNS: (Error) Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.";
285
							break;
286
						case 8:
287
							$status = "phpDynDNS: (Error) Disabled / Locked Hostname.";
288
							break;
289
						case 9:
290
							$status = "phpDynDNS: (Error) Host updated is configured as a web redirect and no update was performed.";
291
							break;
292
						case 10:
293
							$status = "phpDynDNS: (Error) Group supplied does not exist.";
294
							break;
295
						case 11:
296
							$status = "phpDynDNS: (Success) DNS group update is successful.";
297
							break;
298
						case 12:
299
							$status = "phpDynDNS: (Success) DNS group is current, no update performed.";
300
							break;
301
						case 13:
302
							$status = "phpDynDNS: (Error) Update client support not available for supplied hostname or group.";
303
							break;
304
						case 14:
305
							$status = "phpDynDNS: (Error) Hostname supplied does not have offline settings configured.";
306
							break;
307
						case 99:
308
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
309
							break;
310
						case 100:
311
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
312
							break;
313
						default:
314
							$status = "phpDynDNS: (Unknown Response)";
315
							$this->_debug($data);
316
							break;
317
					}
318
					break;
319
				case 'easydns':
320
					if (preg_match('/NOACCESS/i', $data)) {
321
						$status = "phpDynDNS: (Error) Authentication Failed: Username and/or Password was Incorrect.";
322
					} else if (preg_match('/NOSERVICE/i', $data)) {
323
						$status = "phpDynDNS: (Error) No Service: Dynamic DNS Service has been disabled for this domain.";
324
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
325
						$status = "phpDynDNS: (Error) Illegal Input: Self-Explantory";
326
					} else if (preg_match('/TOOSOON/i', $data)) {
327
						$status = "phpDynDNS: (Error) Too Soon: Not Enough Time Has Elapsed Since Last Update";
328
					} else if (preg_match('/NOERROR/i', $data)) {
329
						$status = "phpDynDNS: (Success) IP Updated Successfully!";					
330
					} else {
331
						$status = "phpDynDNS: (Unknown Response)";
332
						$this->_debug($data);
333
					}
334
					break;
335
				case 'hn':
336
					break;
337
				case 'zoneedit':
338
					if (preg_match('/{700,799}/i', $data)) {
339
						$status = "phpDynDNS: (Error) Update Failed!";
340
					} else if (preg_match('/{200,201}/i', $data)) {
341
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
342
					} else {
343
						$status = "phpDynDNS: (Unknown Response)";
344
						$this->_debug($data);
345
					}
346
					break;
347
				case 'dyns':
348
					if (preg_match("/400/i", $data)) {
349
						$status = "phpDynDNS: (Error) Bad Request - The URL was malformed. Required parameters were not provided.";
350
					} else if (preg_match('/402/i', $data)) {
351
						$status = "phpDynDNS: (Error) Update Too Soon - You have tried updating to quickly since last change.";
352
					} else if (preg_match('/403/i', $data)) {
353
						$status = "phpDynDNS: (Error) Database Error - There was a server-sided database error.";
354
					} else if (preg_match('/405/i', $data)) {
355
						$status = "phpDynDNS: (Error) Hostname Error - The hostname (".$this->_dnsHost.") doesn't belong to you.";
356
					} else if (preg_match('/200/i', $data)) {
357
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
358
					} else {
359
						$status = "phpDynDNS: (Unknown Response)";
360
						$this->_debug($data);
361
					}
362
					break;
363
			}
364
			$this->status = $status;
365
			log_error($status);
366
		}
367

    
368

    
369
		/*
370
		 * Private Function (added 12 July 05) [beta]
371
		 *   Return Error, Set Last Error, and Die.
372
		 */
373
		function _error($errorNumber = '1') {
374
			switch ($errorNumber) {
375
				case 0:
376
					break;
377
				case 2:
378
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
379
					break;
380
				case 3:
381
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
382
					break;
383
				case 4:
384
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
385
					break;
386
				case 5:
387
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
388
					break;
389
				case 6:
390
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
391
					break;
392
				case 10:
393
					$error = 'phpDynDNS: No Change In My IP Address and/or 28 Days Has Not Past. Not Updating Dynamic DNS Entry.';
394
					break;
395
				default:
396
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
397
					$this->_debug($data);
398
					break;
399
			}
400
			$this->lastError = $error;
401
			log_error($error);
402
		}
403

    
404

    
405
		/*
406
		 * Private Function (added 12 July 05) [beta]
407
		 *   - Detect whether or not IP needs to be updated.
408
		 *      | Written Specifically for pfSense (pfsense.com) may
409
		 *      | work with other systems. pfSense base is FreeBSD.
410
		 */
411
		function _detectChange() {
412
			$currentTime = time();
413

    
414
			$wan_if = get_real_wan_interface();
415
			$wan_ip = find_interface_ip($wan_if);
416
			$this->_dnsIP = $wan_ip;
417

    
418
			if (file_exists($this->_cacheFile)) {
419
				$file = fopen($this->_cacheFile, 'r');
420
				$contents = fread($file, filesize($this->_cacheFile));
421
				fclose($file);
422
				list($cacheIP,$cacheTime) = split(':', $contents);
423
			} else {
424
				$file = fopen($this->_cacheFile, 'w');
425
				fwrite($file, '0.0.0.0:'.$currentTime);
426
				fclose($file);
427
				$cacheIP = '0.0.0.0';
428
				$cacheTime = $currentTime;
429
			}
430

    
431
			switch ($this->_dnsService) {
432
				case 'dyndns':
433
					$time = '2419200';
434
					break;
435
				case 'dyndns-static':
436
					$time = '2419200';
437
					break;
438
				case 'dyndns-custom':
439
					$time = '2419200';
440
					break;
441
				case 'dhs':
442
					$time = '2419200';
443
					break;
444
				case 'easydns':
445
					$time = '2419200';
446
					break;
447
				case 'noip':
448
					$time = '2419200';
449
					break;
450
				case 'hn':
451
					$time = '2419200';
452
					break;
453
				case 'zoneedit':
454
					$time = '2419200';
455
					break;
456
				case 'dyns':
457
					$time = '2419200';
458
					break;
459
			}
460

    
461
			/* If IP addresses are different or 28 days have passed update record */
462
			if ( ($cacheIP != $wan_ip) || ( ($currentTime - $cacheTime) > $time ) ) {
463
				/* Write WAN IP to cache file */
464
				$file = fopen($this->_cacheFile, 'w');
465
				fwrite($file, $wan_ip.':'.$currentTime);
466
				fclose($file);
467

    
468
				return TRUE;
469
			} else {
470

    
471
				return FALSE;
472
			}
473
		}
474

    
475

    
476
		/*
477
		 * Private Funcation (added 16 July 05) [beta]
478
		 *   - Writes debug information to a file.
479
		 *   - This function is only called when a unknown response
480
		 *   - status is returned from a DynDNS service provider.
481
		 */
482
		function _debug ($data) {
483
			$string = date('m-d-y h:i:s').' - ['.$this->_dnsService.'] - '.$data;
484
			$file = fopen($this->_debugFile, 'a');
485
			fwrite($file, $string);
486
			fclose($file);
487
		}
488

    
489
	}
490

    
491
?>
(4-4/22)