Project

General

Profile

« Previous | Next » 

Revision 858e0d8d

Added by Renato Botelho over 8 years ago

Rework logic and reduce indent levels. No functional changes

View differences:

src/etc/inc/services.inc
2509 2509
	}
2510 2510

  
2511 2511
	/* Dynamic DNS updating active? */
2512
	if (is_array($config['dnsupdates']['dnsupdate'])) {
2513
		$notify_text = "";
2514
		$gwgroups = return_gateway_groups_array();
2515
		foreach ($config['dnsupdates']['dnsupdate'] as $i => $dnsupdate) {
2516
			if (!isset($dnsupdate['enable'])) {
2517
				continue;
2518
			}
2519
			/*
2520
			 * If it's using a gateway group, check if interface is
2521
			 * the active gateway for that group
2522
			 */
2523
			$group_int = '';
2524
			if (is_array($gwgroups[$dnsupdate['interface']])) {
2525
				if (!empty($gwgroups[$dnsupdate['interface']][0]['vip'])) {
2526
					$group_int = $gwgroups[$dnsupdate['interface']][0]['vip'];
2527
				} else {
2528
					$group_int = $gwgroups[$dnsupdate['interface']][0]['int'];
2529
				}
2530
			}
2531
			if (!empty($int) && ($int != $dnsupdate['interface']) && ($int != $group_int)) {
2532
				continue;
2533
			}
2534
			if (!empty($updatehost) && ($updatehost != $dnsupdate['host'])) {
2535
				continue;
2536
			}
2537

  
2538
			/* determine interface name */
2539
			$if = get_failover_interface($dnsupdate['interface']);
2512
	if (!is_array($config['dnsupdates']['dnsupdate'])) {
2513
		return 0;
2514
	}
2540 2515

  
2541
			if (isset($dnsupdate['usepublicip'])) {
2542
				$wanip = dyndnsCheckIP($if);
2516
	$notify_text = "";
2517
	$gwgroups = return_gateway_groups_array();
2518
	foreach ($config['dnsupdates']['dnsupdate'] as $i => $dnsupdate) {
2519
		if (!isset($dnsupdate['enable'])) {
2520
			continue;
2521
		}
2522
		/*
2523
		 * If it's using a gateway group, check if interface is
2524
		 * the active gateway for that group
2525
		 */
2526
		$group_int = '';
2527
		if (is_array($gwgroups[$dnsupdate['interface']])) {
2528
			if (!empty($gwgroups[$dnsupdate['interface']][0]['vip'])) {
2529
				$group_int = $gwgroups[$dnsupdate['interface']][0]['vip'];
2543 2530
			} else {
2544
				$wanip = get_interface_ip($if);
2531
				$group_int = $gwgroups[$dnsupdate['interface']][0]['int'];
2545 2532
			}
2533
		}
2534
		if (!empty($int) && ($int != $dnsupdate['interface']) && ($int != $group_int)) {
2535
			continue;
2536
		}
2537
		if (!empty($updatehost) && ($updatehost != $dnsupdate['host'])) {
2538
			continue;
2539
		}
2546 2540

  
2547
			$wanipv6 = get_interface_ipv6($if);
2548
			$cacheFile = "{$g['conf_path']}/dyndns_{$dnsupdate['interface']}_rfc2136_" . escapeshellarg($dnsupdate['host']) . "_{$dnsupdate['server']}.cache";
2549
			$cacheFilev6 = $cacheFile . ".ipv6";
2550
			$currentTime = time();
2541
		/* determine interface name */
2542
		$if = get_failover_interface($dnsupdate['interface']);
2551 2543

  
2552
			if ($wanip || $wanipv6) {
2553
				$keyname = $dnsupdate['keyname'];
2554
				/* trailing dot */
2555
				if (substr($keyname, -1) != ".") {
2556
					$keyname .= ".";
2557
				}
2544
		if (isset($dnsupdate['usepublicip'])) {
2545
			$wanip = dyndnsCheckIP($if);
2546
		} else {
2547
			$wanip = get_interface_ip($if);
2548
		}
2558 2549

  
2559
				$hostname = $dnsupdate['host'];
2560
				/* trailing dot */
2561
				if (substr($hostname, -1) != ".") {
2562
					$hostname .= ".";
2563
				}
2550
		$wanipv6 = get_interface_ipv6($if);
2551
		$cacheFile = "{$g['conf_path']}/dyndns_{$dnsupdate['interface']}_rfc2136_" . escapeshellarg($dnsupdate['host']) . "_{$dnsupdate['server']}.cache";
2552
		$cacheFilev6 = $cacheFile . ".ipv6";
2553
		$currentTime = time();
2554

  
2555
		if (!$wanip && !$wanipv6) {
2556
			continue;
2557
		}
2558

  
2559
		$keyname = $dnsupdate['keyname'];
2560
		/* trailing dot */
2561
		if (substr($keyname, -1) != ".") {
2562
			$keyname .= ".";
2563
		}
2564

  
2565
		$hostname = $dnsupdate['host'];
2566
		/* trailing dot */
2567
		if (substr($hostname, -1) != ".") {
2568
			$hostname .= ".";
2569
		}
2564 2570

  
2565
				/* write private key file
2566
				   this is dumb - public and private keys are the same for HMAC-MD5,
2567
				   but nsupdate insists on having both */
2568
				$fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.private", "w");
2569
				$privkey = <<<EOD
2571
		/* write private key file
2572
		   this is dumb - public and private keys are the same for HMAC-MD5,
2573
		   but nsupdate insists on having both */
2574
		$fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.private", "w");
2575
		$privkey = <<<EOD
2570 2576
Private-key-format: v1.2
2571 2577
Algorithm: 157 (HMAC)
2572 2578
Key: {$dnsupdate['keydata']}
2573 2579

  
2574 2580
EOD;
2575
				fwrite($fd, $privkey);
2576
				fclose($fd);
2581
		fwrite($fd, $privkey);
2582
		fclose($fd);
2577 2583

  
2578
				/* write public key file */
2579
				if ($dnsupdate['keytype'] == "zone") {
2580
					$flags = 257;
2581
					$proto = 3;
2582
				} else if ($dnsupdate['keytype'] == "host") {
2583
					$flags = 513;
2584
					$proto = 3;
2585
				} else if ($dnsupdate['keytype'] == "user") {
2586
					$flags = 0;
2587
					$proto = 2;
2588
				}
2584
		/* write public key file */
2585
		if ($dnsupdate['keytype'] == "zone") {
2586
			$flags = 257;
2587
			$proto = 3;
2588
		} else if ($dnsupdate['keytype'] == "host") {
2589
			$flags = 513;
2590
			$proto = 3;
2591
		} else if ($dnsupdate['keytype'] == "user") {
2592
			$flags = 0;
2593
			$proto = 2;
2594
		}
2589 2595

  
2590
				$fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.key", "w");
2591
				fwrite($fd, "{$keyname} IN KEY {$flags} {$proto} 157 {$dnsupdate['keydata']}\n");
2592
				fclose($fd);
2596
		$fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.key", "w");
2597
		fwrite($fd, "{$keyname} IN KEY {$flags} {$proto} 157 {$dnsupdate['keydata']}\n");
2598
		fclose($fd);
2593 2599

  
2594
				/* generate update instructions */
2595
				$upinst = "";
2596
				if (!empty($dnsupdate['server'])) {
2597
					$upinst .= "server {$dnsupdate['server']}\n";
2598
				}
2600
		/* generate update instructions */
2601
		$upinst = "";
2602
		if (!empty($dnsupdate['server'])) {
2603
			$upinst .= "server {$dnsupdate['server']}\n";
2604
		}
2599 2605

  
2600
				if (file_exists($cacheFile)) {
2601
					list($cachedipv4, $cacheTimev4) = explode("|", file_get_contents($cacheFile));
2602
				}
2603
				if (file_exists($cacheFilev6)) {
2604
					list($cachedipv6, $cacheTimev6) = explode("|", file_get_contents($cacheFilev6));
2605
				}
2606
		if (file_exists($cacheFile)) {
2607
			list($cachedipv4, $cacheTimev4) = explode("|", file_get_contents($cacheFile));
2608
		}
2609
		if (file_exists($cacheFilev6)) {
2610
			list($cachedipv6, $cacheTimev6) = explode("|", file_get_contents($cacheFilev6));
2611
		}
2606 2612

  
2607
				// 25 Days
2608
				$maxCacheAgeSecs = 25 * 24 * 60 * 60;
2609
				$need_update = false;
2613
		// 25 Days
2614
		$maxCacheAgeSecs = 25 * 24 * 60 * 60;
2615
		$need_update = false;
2610 2616

  
2611
				/* Update IPv4 if we have it. */
2612
				if (is_ipaddrv4($wanip) && $dnsupdate['recordtype'] != "AAAA") {
2613
					if (($wanip != $cachedipv4) || (($currentTime - $cacheTimev4) > $maxCacheAgeSecs) || $forced) {
2614
						$upinst .= "update delete {$dnsupdate['host']}. A\n";
2615
						$upinst .= "update add {$dnsupdate['host']}. {$dnsupdate['ttl']} A {$wanip}\n";
2616
						$need_update = true;
2617
					} else {
2618
						log_error(sprintf(gettext("phpDynDNS: Not updating %s A record because the IP address has not changed."), $dnsupdate['host']));
2619
					}
2620
				} else {
2621
					@unlink($cacheFile);
2622
					unset($cacheFile);
2623
				}
2617
		/* Update IPv4 if we have it. */
2618
		if (is_ipaddrv4($wanip) && $dnsupdate['recordtype'] != "AAAA") {
2619
			if (($wanip != $cachedipv4) || (($currentTime - $cacheTimev4) > $maxCacheAgeSecs) || $forced) {
2620
				$upinst .= "update delete {$dnsupdate['host']}. A\n";
2621
				$upinst .= "update add {$dnsupdate['host']}. {$dnsupdate['ttl']} A {$wanip}\n";
2622
				$need_update = true;
2623
			} else {
2624
				log_error(sprintf(gettext("phpDynDNS: Not updating %s A record because the IP address has not changed."), $dnsupdate['host']));
2625
			}
2626
		} else {
2627
			@unlink($cacheFile);
2628
			unset($cacheFile);
2629
		}
2624 2630

  
2625
				/* Update IPv6 if we have it. */
2626
				if (is_ipaddrv6($wanipv6) && $dnsupdate['recordtype'] != "A") {
2627
					if (($wanipv6 != $cachedipv6) || (($currentTime - $cacheTimev6) > $maxCacheAgeSecs) || $forced) {
2628
						$upinst .= "update delete {$dnsupdate['host']}. AAAA\n";
2629
						$upinst .= "update add {$dnsupdate['host']}. {$dnsupdate['ttl']} AAAA {$wanipv6}\n";
2630
						$need_update = true;
2631
					} else {
2632
						log_error(sprintf(gettext("phpDynDNS: Not updating %s AAAA record because the IPv6 address has not changed."), $dnsupdate['host']));
2633
					}
2634
				} else {
2635
					@unlink($cacheFilev6);
2636
					unset($cacheFilev6);
2637
				}
2631
		/* Update IPv6 if we have it. */
2632
		if (is_ipaddrv6($wanipv6) && $dnsupdate['recordtype'] != "A") {
2633
			if (($wanipv6 != $cachedipv6) || (($currentTime - $cacheTimev6) > $maxCacheAgeSecs) || $forced) {
2634
				$upinst .= "update delete {$dnsupdate['host']}. AAAA\n";
2635
				$upinst .= "update add {$dnsupdate['host']}. {$dnsupdate['ttl']} AAAA {$wanipv6}\n";
2636
				$need_update = true;
2637
			} else {
2638
				log_error(sprintf(gettext("phpDynDNS: Not updating %s AAAA record because the IPv6 address has not changed."), $dnsupdate['host']));
2639
			}
2640
		} else {
2641
			@unlink($cacheFilev6);
2642
			unset($cacheFilev6);
2643
		}
2638 2644

  
2639
				$upinst .= "\n";	/* mind that trailing newline! */
2645
		$upinst .= "\n";	/* mind that trailing newline! */
2640 2646

  
2641
				if ($need_update) {
2642
					@file_put_contents("{$g['varetc_path']}/nsupdatecmds{$i}", $upinst);
2643
					unset($upinst);
2644
					/* invoke nsupdate */
2645
					$cmd = "/usr/local/bin/nsupdate -k {$g['varetc_path']}/K{$i}{$keyname}+157+00000.key";
2646
					if (isset($dnsupdate['usetcp'])) {
2647
						$cmd .= " -v";
2648
					}
2649
					$cmd .= " {$g['varetc_path']}/nsupdatecmds{$i}";
2650
					if (mwexec($cmd) == 0) {
2651
						if (!empty($cacheFile)) {
2652
							@file_put_contents($cacheFile, "{$wanip}|{$currentTime}");
2653
							log_error(sprintf(gettext('phpDynDNS: updating cache file %1$s: %2$s'), $cacheFile, $wanip));
2654
							$notify_text .= sprintf(gettext('DynDNS updated IP Address (A) for %1$s on %2$s (%3$s) to %4$s'), $dnsupdate['host'], convert_real_interface_to_friendly_descr($if), $if, $wanip) . "\n";
2655
						}
2656
						if (!empty($cacheFilev6)) {
2657
							@file_put_contents($cacheFilev6, "{$wanipv6}|{$currentTime}");
2658
							log_error(sprintf(gettext('phpDynDNS: updating cache file %1$s: %2$s'), $cacheFilev6, $wanipv6));
2659
							$notify_text .= sprintf(gettext('DynDNS updated IPv6 Address (AAAA) for %1$s on %2$s (%3$s) to %4$s'), $dnsupdate['host'], convert_real_interface_to_friendly_descr($if), $if, $wanipv6) . "\n";
2660
						}
2661
					} else {
2662
						if (!empty($cacheFile)) {
2663
							log_error(sprintf(gettext('phpDynDNS: ERROR while updating IP Address (A) for %1$s (%2$s)'), $dnsupdate['host'], $wanip));
2664
						}
2665
						if (!empty($cacheFilev6)) {
2666
							log_error(sprintf(gettext('phpDynDNS: ERROR while updating IP Address (AAAA) for %1$s (%2$s)'), $dnsupdate['host'], $wanipv6));
2667
						}
2668
					}
2669
					unset($cmd);
2670
				}
2671
			}
2647
		if (!$need_update) {
2648
			continue;
2649
		}
2650

  
2651
		@file_put_contents("{$g['varetc_path']}/nsupdatecmds{$i}", $upinst);
2652
		unset($upinst);
2653
		/* invoke nsupdate */
2654
		$cmd = "/usr/local/bin/nsupdate -k {$g['varetc_path']}/K{$i}{$keyname}+157+00000.key";
2655
		if (isset($dnsupdate['usetcp'])) {
2656
			$cmd .= " -v";
2672 2657
		}
2673
		if (!empty($notify_text)) {
2674
			notify_all_remote($notify_text);
2658
		$cmd .= " {$g['varetc_path']}/nsupdatecmds{$i}";
2659
		if (mwexec($cmd) == 0) {
2660
			if (!empty($cacheFile)) {
2661
				@file_put_contents($cacheFile, "{$wanip}|{$currentTime}");
2662
				log_error(sprintf(gettext('phpDynDNS: updating cache file %1$s: %2$s'), $cacheFile, $wanip));
2663
				$notify_text .= sprintf(gettext('DynDNS updated IP Address (A) for %1$s on %2$s (%3$s) to %4$s'), $dnsupdate['host'], convert_real_interface_to_friendly_descr($if), $if, $wanip) . "\n";
2664
			}
2665
			if (!empty($cacheFilev6)) {
2666
				@file_put_contents($cacheFilev6, "{$wanipv6}|{$currentTime}");
2667
				log_error(sprintf(gettext('phpDynDNS: updating cache file %1$s: %2$s'), $cacheFilev6, $wanipv6));
2668
				$notify_text .= sprintf(gettext('DynDNS updated IPv6 Address (AAAA) for %1$s on %2$s (%3$s) to %4$s'), $dnsupdate['host'], convert_real_interface_to_friendly_descr($if), $if, $wanipv6) . "\n";
2669
			}
2670
		} else {
2671
			if (!empty($cacheFile)) {
2672
				log_error(sprintf(gettext('phpDynDNS: ERROR while updating IP Address (A) for %1$s (%2$s)'), $dnsupdate['host'], $wanip));
2673
			}
2674
			if (!empty($cacheFilev6)) {
2675
				log_error(sprintf(gettext('phpDynDNS: ERROR while updating IP Address (AAAA) for %1$s (%2$s)'), $dnsupdate['host'], $wanipv6));
2676
			}
2675 2677
		}
2678
		unset($cmd);
2679
	}
2680

  
2681
	if (!empty($notify_text)) {
2682
		notify_all_remote($notify_text);
2676 2683
	}
2677 2684

  
2678 2685
	return 0;

Also available in: Unified diff