From e13dd2cb044afdf2858bb2252bbad14c14221e4e Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 8 Sep 2020 14:39:49 -0700 Subject: [PATCH] aredn: allow aliased hostnames on the mesh Allows for aliased hostnames on the mesh. One IP/Host can be assigned to multiple hostnames. This is useful for many things including virtual hosts, virtual machines, virtual email addresses, etc. The possibilities are actually _endless_. Fixes #516 --- files/etc/arednsysupgrade.conf | 1 + files/usr/local/bin/node-setup | 12 ++- files/usr/local/bin/olsrd-config | 6 ++ files/www/cgi-bin/ports | 141 +++++++++++++++++++++++++++++-- 4 files changed, 151 insertions(+), 9 deletions(-) diff --git a/files/etc/arednsysupgrade.conf b/files/etc/arednsysupgrade.conf index f4b26158..85187e36 100644 --- a/files/etc/arednsysupgrade.conf +++ b/files/etc/arednsysupgrade.conf @@ -7,6 +7,7 @@ /etc/config.mesh/_setup.ports.nat /etc/config.mesh/_setup.services.dmz /etc/config.mesh/_setup.services.nat +/etc/config.mesh/aliases /etc/config.mesh/vtun /etc/config.mesh/aredn /etc/dropbear/dropbear_dss_host_key diff --git a/files/usr/local/bin/node-setup b/files/usr/local/bin/node-setup index 31b7d05b..ad29511a 100755 --- a/files/usr/local/bin/node-setup +++ b/files/usr/local/bin/node-setup @@ -192,6 +192,7 @@ $portfile = "/etc/config.mesh/_setup.ports"; $dhcpfile = "/etc/config.mesh/_setup.dhcp"; $portfile .= ($cfg{dmz_mode} ? ".dmz" : ".nat"); $dhcpfile .= ($cfg{dmz_mode} ? ".dmz" : ".nat"); +$aliasfile = "/etc/config.mesh/aliases"; # basic configuration @@ -300,7 +301,16 @@ foreach(`cat $dhcpfile`) printf ETHER "$mac\t$ip $noprop\n"; printf HOSTS "$ip\t$host\n"; } - +#aliases need to be added to /etc/hosts or they will not show up on the localnode +#nor will the services they might offer +if(-e $aliasfile) { + foreach(`cat $aliasfile`) { + next if /^\s*#/; + next if /^\s*$/; + ($ip, $host) = split /\s+/, $_; + printf HOSTS "$ip\t$host\n"; + } +} print HOSTS "\n"; close(HOSTS); close(ETHER); diff --git a/files/usr/local/bin/olsrd-config b/files/usr/local/bin/olsrd-config index 29b2893f..55f558b8 100755 --- a/files/usr/local/bin/olsrd-config +++ b/files/usr/local/bin/olsrd-config @@ -66,6 +66,12 @@ push @names, $name if ($name = nvram_get("node")); # load the dhcp reservations when in dmz mode if(-f "/etc/config/dmz-mode") { + # add DNS aliases first + # (see above comment about "tactical" names) + foreach(`cat /etc/config.mesh/aliases`) { + next unless ($ip, $host) = split ' ', $_; + push @hosts, qq("$ip" "$host"); + } #($lanip, $lanmask, $lanbcast, $lannet) = get_ip4_network("eth0.0"); foreach(`cat /etc/ethers`) { diff --git a/files/www/cgi-bin/ports b/files/www/cgi-bin/ports index 74b586e0..996c995a 100755 --- a/files/www/cgi-bin/ports +++ b/files/www/cgi-bin/ports @@ -89,11 +89,13 @@ system "mkdir -p $tmpdir"; $portfile = "/etc/config.mesh/_setup.ports"; $dhcpfile = "/etc/config.mesh/_setup.dhcp"; $servfile = "/etc/config.mesh/_setup.services"; +$aliasfile = "/etc/config.mesh/aliases"; my $suffix = $dmz_mode ? ".dmz" : ".nat"; $portfile .= $suffix; $dhcpfile .= $suffix; $servfile .= $suffix; +#do not need mesh aliases in anything other than "dmz_mode" # if a reset or a first time page load # read the data from the config files @@ -163,12 +165,27 @@ if($parms{button_reset} or not $parms{reload}) } $parms{serv_num} = $i; + #aliases + $i = 0; + foreach(`cat $aliasfile 2>/dev/null`) + { + next if /^\s*#/; + next if /^\s*$/; + chomp; + @parts = split /\s+/, $_; + next unless scalar(@parts) == 2; + ++$i; + $parms{"alias${i}_host"} = $parts[1]; + $parms{"alias${i}_ip"} = $parts[0]; + } + $parms{alias_num} = $i; + # sanitize the "add" values $parms{port_add_intf} = $dmz_mode ? "wan" : "wifi"; $parms{port_add_type} = "tcp"; $parms{dmz_ip} = "" unless defined $parms{dmz_ip}; - foreach $var (qw(port_add_out port_add_ip port_add_in dhcp_add_host dhcp_add_ip dhcp_add_mac dhcp_add_noprop serv_add_name serv_add_proto serv_add_host serv_add_port serv_add_suffix)) + foreach $var (qw(port_add_out port_add_ip port_add_in dhcp_add_host dhcp_add_ip dhcp_add_mac dhcp_add_noprop serv_add_name serv_add_proto serv_add_host serv_add_port serv_add_suffix alias_add_host alias_add_ip)) { $parms{$var} = ""; } @@ -456,6 +473,64 @@ for($i = $nn = 1; $i <= $parms{dhcp_num}; $i++) } close(FILE); +# +# aliases +# +for($i = 1, @list = (); $i <= $parms{alias_num}; ++$i) { push @list, $i } +push @list, "_add"; +$alias_num = 0; +foreach $val (@list) { + $host = $parms{"alias${val}_host"}; + $ip = $parms{"alias${val}_ip"}; + #if adding alias check the name is not already in use, + #also check that it does not contain anything that will be weird on the mesh + #for instance: supercoolservice.kg6wxc-host.local.mesh is certainly a valid host name, but it won't work for the mesh. + if($val eq "_add") { + if($host) { + my $olsrFile = 0; + $olsrFile = 1 if -f "/var/run/hosts_olsr"; + if($olsrFile) { + open(my $hostFile, "<", "/var/run/hosts_olsr"); + while(<$hostFile>) { + if($_ =~ /\s$host\s/i) { + $foundHost = 1; + last; + } + } + close($hostFile); + push(@alias_err, "$val Warning! '$host' is already in use!
" . + "Please choose another alias name.
" . + "Prefixing the hostname with your callsign will help prevent duplicates on the network.") if $foundHost == 1; + } + push(@alias_err, "$val Warning! The alias name: '$host' is invalid") if !validate_hostname($host); + push(@alias_err, "$val '$host' cannot contain the dot '.' character!") if index($host, ".") != -1; + } + next unless ($host or $ip or $foundHost) and ($parms{alias_add} or $parms{button_save}); + } else { + next if $parms{"alias${val}_del"}; + } + if($val eq "_add" and $parms{button_save}) { + push(@alias_err, "$val this alias must be added or cleared out before saving changes"); + next; + } + next if $val eq "_add" and @alias_err and $alias_err[-1] =~ /^$val /; + # commit the data for this alias + ++$alias_num; + $parms{"alias${alias_num}_host"} = $host; + $parms{"alias${alias_num}_ip"} = $ip; + $hosts{$host} = 1; + if($val eq "_add") { + $parms{alias_add_host} = ""; + $parms{alias_add_ip} = ""; + } +} +#write to temp file +open(FILE, ">$tmpdir/aliases"); +for($i = 1, @list = (); $i <= $alias_num; ++$i) { + printf FILE "%s %s\n", $parms{"alias${i}_ip"}, $parms{"alias${i}_host"}; +} +close(FILE); +$parms{alias_num} = $alias_num; # # load and validate the services @@ -549,16 +624,17 @@ foreach $val (@list) close(FILE); $parms{serv_num} = $serv_num; - # # save configuration # -if($parms{button_save} and not (@port_err or @dhcp_err or @dmz_err or @serv_err)) +if($parms{button_save} and not (@port_err or @dhcp_err or @dmz_err or @serv_err or @alias_err)) { system "cp -f $tmpdir/ports $portfile"; system "cp -f $tmpdir/dhcp $dhcpfile"; system "cp -f $tmpdir/services $servfile"; + system "cp -f $tmpdir/aliases $aliasfile"; + push(@errors, "problem with configuration") if system "/usr/local/bin/node-setup -a -p mesh"; unless($debug == 3) @@ -633,16 +709,20 @@ if($dmz_mode) print "\n"; &print_reservations(); print "\n"; - print "     \n"; &print_services(); print "\n"; - print "\n"; print " \n"; - print "
\n"; - print "\n"; + print "
\n"; + print "\n"; + print "\n"; + print "\n"; + print "\n"; + print "\n"; } else @@ -669,6 +749,7 @@ print "
\n"; &print_forwarding(); + print "          \n"; + &print_aliases(); print "
\n"; push @hidden, ""; push @hidden, ""; push @hidden, ""; +push @hidden, ""; foreach(@hidden) { print "$_\n" } print "\n"; @@ -880,7 +961,7 @@ sub print_services unless($dmz_mode or $parms{port_num} or $parms{dmz_ip}) { if($dmz_mode) { print " \n" } - else { print " 

", "\n" } + else { print " 

", "\n" } print "none\n"; print "\n"; return; @@ -921,6 +1002,9 @@ sub print_services print " disabled" unless $val eq "_add" or $link; print ">\n"; selopt($node, $node, $host); + for($i = 1; $i <= $parms{alias_num}; $i++) { + selopt($parms{"alias${i}_host"}, $parms{"alias${i}_host"}, $host); + } for($i = 1; $i <= $parms{dhcp_num}; $i++) { selopt($parms{"dhcp${i}_host"}, $parms{"dhcp${i}_host"}, $host); @@ -966,3 +1050,44 @@ sub print_services print "\n"; } +# aliases +sub print_aliases { + print "\n"; + print "\n"; + print "\n"; + print "\n"; + for($i = 1, @list = (); $i <= $parms{alias_num}; ++$i) { push @list, $i } + push @list, "_add"; + foreach $val (@list) { + $host = $parms{"alias${val}_host"}; + $ip = $parms{"alias${val}_ip"}; + print "\n" if $val eq "_add" and scalar(@list) > 1; + print "\n"; + print ""; + print "\n"; + print "\n"; + } + while(@alias_err) + { + $err = shift @alias_err; + $err =~ s/^\S+ //; + print "\n"; + } + print "
DNS Aliases
Alias NameIP Address
      
$err
\n"; +}