diff --git a/Anvil/Tools.t b/Anvil/Tools.t index 60f8eebc..e0174e9e 100755 --- a/Anvil/Tools.t +++ b/Anvil/Tools.t @@ -517,20 +517,20 @@ unlink $test_template_file; ### Anvil::Tools::Validate tests # is_ipv4 -is($anvil->Validate->is_ipv4({ip => "0.0.0.0"}), "1", "Verifying that 'Validate->is_ipv4' recognizes '0.0.0.0' as a valid IP address."); -is($anvil->Validate->is_ipv4({ip => "255.255.255.255"}), "1", "Verifying that 'Validate->is_ipv4' recognizes '255.255.255.255' as a valid IP address."); -is($anvil->Validate->is_ipv4({ip => "256.255.255.255"}), "0", "Verifying that 'Validate->is_ipv4' recognizes '256.255.255.255' as an invalid IP address."); -is($anvil->Validate->is_ipv4({ip => "alteeve.com"}), "0", "Verifying that 'Validate->is_ipv4' recognizes 'alteeve.com' as an invalid IP address."); -is($anvil->Validate->is_ipv4({ip => "::1"}), "0", "Verifying that 'Validate->is_ipv4' recognizes '::1' as an invalid IP address."); +is($anvil->Validate->ipv4({ip => "0.0.0.0"}), "1", "Verifying that 'Validate->ipv4' recognizes '0.0.0.0' as a valid IP address."); +is($anvil->Validate->ipv4({ip => "255.255.255.255"}), "1", "Verifying that 'Validate->ipv4' recognizes '255.255.255.255' as a valid IP address."); +is($anvil->Validate->ipv4({ip => "256.255.255.255"}), "0", "Verifying that 'Validate->ipv4' recognizes '256.255.255.255' as an invalid IP address."); +is($anvil->Validate->ipv4({ip => "alteeve.com"}), "0", "Verifying that 'Validate->ipv4' recognizes 'alteeve.com' as an invalid IP address."); +is($anvil->Validate->ipv4({ip => "::1"}), "0", "Verifying that 'Validate->ipv4' recognizes '::1' as an invalid IP address."); my $test_uuid = $anvil->Get->uuid; -is($anvil->Validate->is_uuid({uuid => $test_uuid}), "1", "Verifying that 'Validate->is_uuid' recognized: [".$test_uuid."] as a valid UUID."); +is($anvil->Validate->uuid({uuid => $test_uuid}), "1", "Verifying that 'Validate->uuid' recognized: [".$test_uuid."] as a valid UUID."); my $bad_uuid_1 = $test_uuid; $bad_uuid_1 =~ s/-//g; -is($anvil->Validate->is_uuid({uuid => $bad_uuid_1}), "0", "Verifying that 'Validate->is_uuid' recognized: [".$bad_uuid_1."] as an invalid UUID."); +is($anvil->Validate->uuid({uuid => $bad_uuid_1}), "0", "Verifying that 'Validate->uuid' recognized: [".$bad_uuid_1."] as an invalid UUID."); my $bad_uuid_2 = uc($test_uuid); -is($anvil->Validate->is_uuid({uuid => $bad_uuid_2}), "0", "Verifying that 'Validate->is_uuid' recognized: [".$bad_uuid_2."] as an invalid UUID."); +is($anvil->Validate->uuid({uuid => $bad_uuid_2}), "0", "Verifying that 'Validate->uuid' recognized: [".$bad_uuid_2."] as an invalid UUID."); my $bad_uuid_3 = $test_uuid."toolong"; -is($anvil->Validate->is_uuid({uuid => $bad_uuid_3}), "0", "Verifying that 'Validate->is_uuid' recognized: [".$bad_uuid_3."] as an invalid UUID."); +is($anvil->Validate->uuid({uuid => $bad_uuid_3}), "0", "Verifying that 'Validate->uuid' recognized: [".$bad_uuid_3."] as an invalid UUID."); ### Anvil::Tools::Words tests # clean_spaces diff --git a/Anvil/Tools/Account.pm b/Anvil/Tools/Account.pm index b99ab6a9..03330a87 100644 --- a/Anvil/Tools/Account.pm +++ b/Anvil/Tools/Account.pm @@ -649,7 +649,7 @@ sub read_details my $user_uuid = defined $parameter->{user_uuid} ? $parameter->{user_uuid} : $anvil->data->{cookie}{anvil_user_uuid}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { user_uuid => $user_uuid }}); - if (not $anvil->Validate->is_uuid({uuid => $user_uuid})) + if (not $anvil->Validate->uuid({uuid => $user_uuid})) { $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "error_0025", variables => { uuid => $user_uuid }}); return(0); @@ -937,7 +937,7 @@ sub _build_cookie_hash uuid => $uuid, }}); - if (not $anvil->Validate->is_uuid({uuid => $uuid})) + if (not $anvil->Validate->uuid({uuid => $uuid})) { $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Account->_build_cookie_hash()", parameter => "uuid" }}); return(0, 0); diff --git a/Anvil/Tools/Convert.pm b/Anvil/Tools/Convert.pm index e9c4be92..269a720e 100644 --- a/Anvil/Tools/Convert.pm +++ b/Anvil/Tools/Convert.pm @@ -565,7 +565,7 @@ sub cidr elsif ($cidr eq "31") { $output = "255.255.255.254"; } elsif ($cidr eq "32") { $output = "255.255.255.255"; } } - elsif ($anvil->Validate->is_ipv4({ip => $subnet_mask})) + elsif ($anvil->Validate->ipv4({ip => $subnet_mask})) { if ($subnet_mask eq "0.0.0.0" ) { $output = "0"; } elsif ($subnet_mask eq "128.0.0.0" ) { $output = "1"; } @@ -642,7 +642,7 @@ sub host_name_to_ip foreach my $line (split/\n/, $output) { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { line => $line }}); - if ($anvil->Validate->is_ipv4({ip => $line})) + if ($anvil->Validate->ipv4({ip => $line})) { $ip = $line; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { ip => $ip }}); diff --git a/Anvil/Tools/Database.pm b/Anvil/Tools/Database.pm index 61683c21..6a8d8677 100644 --- a/Anvil/Tools/Database.pm +++ b/Anvil/Tools/Database.pm @@ -857,7 +857,7 @@ sub connect { # Periodically, autovivication causes and empty key to appear. $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { uuid => $uuid }}); - next if ((not $uuid) or (not $anvil->Validate->is_uuid({uuid => $uuid}))); + next if ((not $uuid) or (not $anvil->Validate->uuid({uuid => $uuid}))); if (($db_uuid) && ($db_uuid ne $uuid)) { @@ -5881,7 +5881,7 @@ sub insert_or_update_jobs } # Make sure I have the either a valid job UUID or a name - if ((not $anvil->Validate->is_uuid({uuid => $job_uuid})) && (not $job_name)) + if ((not $anvil->Validate->uuid({uuid => $job_uuid})) && (not $job_name)) { $anvil->Log->entry({source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__, level => 0, priority => "err", key => "log_0136", variables => { method => "Database->insert_or_update_jobs()", @@ -6982,13 +6982,13 @@ sub insert_or_update_network_interfaces $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_network_interfaces()", parameter => "network_interface_name" }}); return(""); } - if (($network_interface_bond_uuid ne 'NULL') && (not $anvil->Validate->is_uuid({uuid => $network_interface_bond_uuid}))) + if (($network_interface_bond_uuid ne 'NULL') && (not $anvil->Validate->uuid({uuid => $network_interface_bond_uuid}))) { # Bad UUID. $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0130", variables => { method => "Database->insert_or_update_network_interfaces()", parameter => "network_interface_bond_uuid", uuid => $network_interface_bond_uuid }}); return(""); } - if (($network_interface_bridge_uuid ne 'NULL') && (not $anvil->Validate->is_uuid({uuid => $network_interface_bridge_uuid}))) + if (($network_interface_bridge_uuid ne 'NULL') && (not $anvil->Validate->uuid({uuid => $network_interface_bridge_uuid}))) { # Bad UUID. $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0130", variables => { method => "Database->insert_or_update_network_interfaces()", parameter => "network_interface_bridge_uuid", uuid => $network_interface_bridge_uuid }}); @@ -7531,7 +7531,7 @@ sub insert_or_update_mac_to_ip } # If the MAC isn't 12 or 17 bytes long (18 being xx:xx:xx:xx:xx:xx), or isn't a valid hex string, abort. - if (((length($mac_to_ip_mac_address) != 12) && (length($mac_to_ip_mac_address) != 17)) or (not $anvil->Validate->is_hex({debug => $debug, string => $mac_to_ip_mac_address, sloppy => 1}))) + if (((length($mac_to_ip_mac_address) != 12) && (length($mac_to_ip_mac_address) != 17)) or (not $anvil->Validate->hex({debug => $debug, string => $mac_to_ip_mac_address, sloppy => 1}))) { $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "error_0096", variables => { mac_to_ip_mac_address => $mac_to_ip_mac_address }}); return(""); @@ -7740,7 +7740,7 @@ sub insert_or_update_oui } # If the MAC isn't 6 or 8 bytes long (8 being xx:xx:xx), or isn't a valid hex string, abort. - if (((length($oui_mac_prefix) != 6) && (length($oui_mac_prefix) != 8)) or (not $anvil->Validate->is_hex({debug => $debug, string => $oui_mac_prefix, sloppy => 1}))) + if (((length($oui_mac_prefix) != 6) && (length($oui_mac_prefix) != 8)) or (not $anvil->Validate->hex({debug => $debug, string => $oui_mac_prefix, sloppy => 1}))) { $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "error_0096", variables => { oui_mac_prefix => $oui_mac_prefix }}); return(""); @@ -9223,7 +9223,7 @@ sub insert_or_update_variables # If we have a variable UUID but not a name, read the variable name. If we don't have a UUID, see if # we can find one for the given variable name. - if (($anvil->Validate->is_uuid({uuid => $variable_uuid})) && (not $variable_name)) + if (($anvil->Validate->uuid({uuid => $variable_uuid})) && (not $variable_name)) { my $query = " SELECT @@ -9880,7 +9880,7 @@ sub manage_anvil_conf $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->manage_anvil_conf()", parameter => "db_host_uuid" }}); return(1); } - elsif (not $anvil->Validate->is_uuid({uuid => $db_host_uuid})) + elsif (not $anvil->Validate->uuid({uuid => $db_host_uuid})) { $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "error_0031", variables => { db_host_uuid => $db_host_uuid }}); return(1); diff --git a/Anvil/Tools/Get.pm b/Anvil/Tools/Get.pm index 80315e05..951cfdf3 100644 --- a/Anvil/Tools/Get.pm +++ b/Anvil/Tools/Get.pm @@ -859,7 +859,7 @@ sub host_uuid return("#!error!#"); } - if ($anvil->Validate->is_uuid({uuid => $uuid})) + if ($anvil->Validate->uuid({uuid => $uuid})) { $anvil->{HOST}{UUID} = $uuid; if (not -e $anvil->data->{path}{data}{host_uuid}) diff --git a/Anvil/Tools/Job.pm b/Anvil/Tools/Job.pm index e00376ea..388fb126 100644 --- a/Anvil/Tools/Job.pm +++ b/Anvil/Tools/Job.pm @@ -209,7 +209,7 @@ AND } } - if (not $anvil->Validate->is_uuid({uuid => $anvil->data->{switches}{'job-uuid'}})) + if (not $anvil->Validate->uuid({uuid => $anvil->data->{switches}{'job-uuid'}})) { # It's not a UUID. $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 0, secure => 0, key => "error_0033", variables => { uuid => $anvil->data->{switches}{'job-uuid'} } }); diff --git a/Anvil/Tools/Network.pm b/Anvil/Tools/Network.pm index 65d32009..58791267 100755 --- a/Anvil/Tools/Network.pm +++ b/Anvil/Tools/Network.pm @@ -300,8 +300,8 @@ sub check_internet # Is the domain valid? $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { domain => $domain }}); - if ((not $anvil->Validate->is_domain_name({debug => $debug, name => $domain})) and - (not $anvil->Validate->is_ipv4({debug => $debug, ip => $domain}))) + if ((not $anvil->Validate->domain_name({debug => $debug, name => $domain})) and + (not $anvil->Validate->ipv4({debug => $debug, ip => $domain}))) { # Not valid, skip $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0442", variables => { name => $domain }}); @@ -1394,7 +1394,7 @@ sub get_company_from_mac return($anvil->data->{cache}{mac_to_oui}{$mac}); } - my $valid_mac = $anvil->Validate->is_mac({mac => $mac}); + my $valid_mac = $anvil->Validate->mac({mac => $mac}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { mac => $mac }}); if ($valid_mac) { @@ -1893,7 +1893,7 @@ sub get_network my $base = $block->base(); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { base => $base }}); - if ($anvil->Validate->is_ipv4({ip => $base})) + if ($anvil->Validate->ipv4({ip => $base})) { $network = $base; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { network => $network }}); @@ -2006,7 +2006,7 @@ sub is_ip_in_network $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Network->is_ip_in_network()", parameter => "network" }}); return(0); } - elsif (not $anvil->Validate->is_ipv4({ip => $network})) + elsif (not $anvil->Validate->ipv4({ip => $network})) { $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "warning_0019", variables => { parameter => "network", network => $network }}); return(0); @@ -2016,7 +2016,7 @@ sub is_ip_in_network $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Network->is_ip_in_network()", parameter => "ip" }}); return(0); } - elsif (not $anvil->Validate->is_ipv4({ip => $ip})) + elsif (not $anvil->Validate->ipv4({ip => $ip})) { $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "warning_0019", variables => { parameter => "ip", network => $ip }}); return(0); @@ -2026,7 +2026,7 @@ sub is_ip_in_network $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Network->is_ip_in_network()", parameter => "subnet_mask" }}); return(0); } - elsif (not $anvil->Validate->is_subnet_mask({subnet_mask => $subnet_mask})) + elsif (not $anvil->Validate->subnet_mask({subnet_mask => $subnet_mask})) { $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "warning_0020", variables => { parameter => "subnet_mask", subnet_mask => $subnet_mask }}); return(0); diff --git a/Anvil/Tools/Remote.pm b/Anvil/Tools/Remote.pm index a2fee082..27670c1f 100644 --- a/Anvil/Tools/Remote.pm +++ b/Anvil/Tools/Remote.pm @@ -396,7 +396,7 @@ sub call } # If the target is a host name, convert it to an IP. - if (not $anvil->Validate->is_ipv4({ip => $target})) + if (not $anvil->Validate->ipv4({ip => $target})) { my $new_target = $anvil->Convert->host_name_to_ip({host_name => $target}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { new_target => $new_target }}); @@ -668,6 +668,21 @@ sub call { $return_code = $1; } + elsif ($line =~ /return_code:(\d+)$/) + { + ### NOTE: This should never happen given we have a newline before the echo, + ### but it's here just in case. + # If the output of the shell call doesn't end in a newline, the return_code:X + # could be appended. This catches those cases and removes it. + $return_code = $1; + $line =~ s/return_code:\d+$//; + $output .= $line."\n"; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { + line => $line, + output => $output, + return_code => $return_code, + }}); + } else { $clean_output .= $line."\n"; diff --git a/Anvil/Tools/Striker.pm b/Anvil/Tools/Striker.pm index 6e86e918..02b11cde 100644 --- a/Anvil/Tools/Striker.pm +++ b/Anvil/Tools/Striker.pm @@ -670,7 +670,7 @@ sub get_peer_data $anvil->Database->write({uuid => $anvil->data->{sys}{host_uuid}, debug => 3, query => $query, source => $THIS_FILE, line => __LINE__}); # Verify that the host UUID is actually valid. - if (not $anvil->Validate->is_uuid({uuid => $data->{host_uuid}})) + if (not $anvil->Validate->uuid({uuid => $data->{host_uuid}})) { $data->{host_uuid} = ""; } @@ -817,7 +817,7 @@ sub load_manifest $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Striker->load_manifest()", parameter => "manifest_uuid" }}); return(1); } - elsif (not $anvil->Validate->is_uuid({uuid => $manifest_uuid})) + elsif (not $anvil->Validate->uuid({uuid => $manifest_uuid})) { # UUID isn't valid $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0130", variables => { method => "Striker->load_manifest()", parameter => "manifest_uuid", uuid => $manifest_uuid}}); diff --git a/Anvil/Tools/System.pm b/Anvil/Tools/System.pm index 91c40a96..a54b895d 100644 --- a/Anvil/Tools/System.pm +++ b/Anvil/Tools/System.pm @@ -127,7 +127,7 @@ sub activate_lv my $path = defined $parameter->{path} ? $parameter->{path} : ""; my $activated = 0; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { path => $path, }}); @@ -144,7 +144,7 @@ sub activate_lv } my ($output, $return_code) = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{lvchange}." --activate y ".$path}); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { output => $output, return_code => $return_code, }}); @@ -154,7 +154,7 @@ sub activate_lv # Check if it worked. $activated = $anvil->data->{lvm}{'local'}{lv}{$path}{active}; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { activated => $activated }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { activated => $activated }}); return($activated); } @@ -341,7 +341,7 @@ sub call if ($line =~ /^return_code:(\d+)$/) { $return_code = $1; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { return_code => $return_code }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { return_code => $return_code }}); } elsif ($line =~ /return_code:(\d+)$/) { @@ -351,7 +351,7 @@ sub call $return_code = $1; $line =~ s/return_code:\d+$//; $output .= $line."\n"; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { line => $line, output => $output, return_code => $return_code, @@ -360,13 +360,13 @@ sub call else { $output .= $line."\n"; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { output => $output }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { output => $output }}); } } close $file_handle; chomp($output); $output =~ s/\n$//s; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { output => $output }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { output => $output }}); } } } @@ -429,7 +429,7 @@ sub change_shell_user_password my $target = defined $parameter->{target} ? $parameter->{target} : ""; my $user = defined $parameter->{user} ? $parameter->{user} : ""; my $return_code = 255; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { user => $user, target => $target, port => $port, @@ -466,7 +466,7 @@ sub change_shell_user_password # Generate a salt and then use it to create a hash. (my $salt, $return_code) = $anvil->System->call({debug => $debug, shell_call => $anvil->data->{path}{exe}{openssl}." rand 1000 | ".$anvil->data->{path}{exe}{strings}." | ".$anvil->data->{path}{exe}{'grep'}." -io [0-9A-Za-z\.\/] | ".$anvil->data->{path}{exe}{head}." -n 16 | ".$anvil->data->{path}{exe}{'tr'}." -d '\\n'" }); my $new_hash = crypt($new_password,"\$6\$".$salt."\$"); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { salt => $salt, new_hash => $new_hash, return_code => $return_code, @@ -1689,7 +1689,7 @@ sub find_matching_ip } # Translate the host name to an IP address, if it isn't already an IP address. - if (not $anvil->Validate->is_ipv4({ip => $host})) + if (not $anvil->Validate->ipv4({ip => $host})) { # This will be '0' if it failed, and pre-validated if it returns an IP. $host = $anvil->Convert->host_name_to_ip({host_name => $host}); @@ -2956,7 +2956,7 @@ sub stty_echo if ($set eq "off") { ($anvil->data->{sys}{terminal}{stty}, my $return_code) = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{stty}." --save"}); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { 'sys::terminal::stty' => $anvil->data->{sys}{terminal}{stty}, return_code => $return_code }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { 'sys::terminal::stty' => $anvil->data->{sys}{terminal}{stty}, return_code => $return_code }}); $anvil->System->call({shell_call => $anvil->data->{path}{exe}{stty}." -echo"}); } elsif (($set eq "on") && ($anvil->data->{sys}{terminal}{stty})) @@ -2992,7 +2992,7 @@ sub update_hosts my $short_host_name = $host_name; $short_host_name =~ s/\..*$//; my $host_type = $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_type}; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { 's1:host_name' => $host_name, 's2:short_host_name' => $short_host_name, 's3:host_type' => $host_type, @@ -3001,7 +3001,7 @@ sub update_hosts # We store this in a way that lets us later sort by type -> host_name $anvil->data->{trusted_host}{$host_type}{$short_host_name}{host_name} = $host_name; $anvil->data->{trusted_host}{$host_type}{$short_host_name}{host_uuid} = $host_uuid; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "trusted_host::${host_type}::${short_host_name}::host_name" => $anvil->data->{trusted_host}{$host_type}{$short_host_name}{host_name}, "trusted_host::${host_type}::${short_host_name}::host_uuid" => $anvil->data->{trusted_host}{$host_type}{$short_host_name}{host_uuid}, }}); @@ -3019,20 +3019,225 @@ sub update_hosts }}); $anvil->data->{trusted_host}{$host_type}{$short_host_name}{network}{$network_type}{$sequence}{ip_address} = $ip_address; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, list => { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "trusted_host::${host_type}::${short_host_name}::network::${network_type}::${sequence}::ip_address" => $anvil->data->{trusted_host}{$host_type}{$short_host_name}{network}{$network_type}{$sequence}{ip_address}, }}); + + # Store the hostname in an easy to lookup format, too. + my $store_host_name = $short_host_name.".".$on_network; + $anvil->data->{hosts}{needed}{$store_host_name}{ip_address} = $ip_address; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + "hosts::needed::${store_host_name}::ip_address" => $anvil->data->{hosts}{needed}{$store_host_name}{ip_address}, + }}); + + # If this is BCN 1, store the full and short host names as well. + if ($on_network eq "bcn1") + { + # If the host name and short host name, this will be duplicate. No harm... + $anvil->data->{hosts}{needed}{$host_name}{ip_address} = $ip_address; + $anvil->data->{hosts}{needed}{$short_host_name}{ip_address} = $ip_address; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + "hosts::needed::${host_name}::ip_address" => $anvil->data->{hosts}{needed}{$host_name}{ip_address}, + "hosts::needed::${short_host_name}::ip_address" => $anvil->data->{hosts}{needed}{$short_host_name}{ip_address}, + }}); + } } } - die; # Read in the existing hosts file - my $old_body = $anvil->Storage->read_file({ + my $add_header = 1; + my $changes = 0; + my $new_body = ""; + my $old_body = $anvil->Storage->read_file({ debug => $debug, file => $anvil->data->{path}{configs}{hosts}, }); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { old_body => $old_body }}); # Parse the existing + foreach my $line (split/\n/, $old_body) + { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { line => $line }}); + + # Delete everything follow a hash, then clear spaces. + my $line_comment = ""; + my $line_hosts = ""; + if ($line =~ /^#/) + { + $new_body .= $line."\n"; + next; + } + if ($line =~ /#(.*)$/) + { + $line_comment = $1; + } + $line =~ s/^\s+//; + $line =~ s/\s+$//; + next if not $line; + + # Now pull apart the line and store the entries. + my ($ip_address, $names) = ($line =~ /^(.*?)\s+(.*)$/); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + 's1:ip_address' => $ip_address, + 's2:names' => $names, + }}); + + # Make sure the IP is valid. + my $is_ip = $anvil->Validate->ip({ip => $ip_address, debug => $debug}); + if (not $is_ip) + { + # Log and skip. + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, key => "warning_0051", variables => { + ip => $ip_address, + names => $names, + }}); + next; + } + + foreach my $name (split/\s+/, $names) + { + if ($line =~ /##] anvil-daemon \[##/) + { + $add_header = 0; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { add_header => $add_header }}); + } + + # Is this name one we manage? If so, has the IP changed? + if ((exists $anvil->data->{hosts}{needed}{$name}) && ($anvil->data->{hosts}{needed}{$name}{ip_address})) + { + my $current_ip = $anvil->data->{hosts}{needed}{$name}{ip_address}; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + ip_address => $ip_address, + current_ip => $current_ip, + }}); + if ($current_ip eq $ip_address) + { + # Matches, we don't need to deal with this name. + delete $anvil->data->{hosts}{needed}{$name}; + } + else + { + # The IP has changed. Skip this name (which removes it from the list). + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0481", variables => { + old_ip => $current_ip, + new_ip => $ip_address, + name => $name, + }}); + $changes = 1; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { changes => $changes }}); + next; + } + } + else + { + $line_hosts .= $name." "; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { line_hosts => $line_hosts }}); + } + } + + # If we have any names for this IP, store it. + if ($line_hosts) + { + my $new_line .= $ip_address." ".$line_hosts; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { new_line => $new_line }}); + if ($line_comment) + { + $new_line .= "\t# ".$line_comment."\n"; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { new_line => $new_line }}); + } + else + { + $new_line =~ s/\s+$//; + $new_line .= "\n"; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { new_line => $new_line }}); + } + $new_body .= $new_line; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { new_body => $new_body }}); + } + } + + # Do we need to pre-pend the header? + if ($add_header) + { + # Prepend the header. + my $header = $anvil->Words->string({key => "message_0177"}); + $new_body = $header.$new_body; + $changes = 1; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + 's1:changes' => $changes, + 's2:header' => $header, + 's3:new_body' => $new_body, + }}); + } + + # Now add any hosts we still need. + my $ip_order = []; + my $lines = {}; + foreach my $host_type ("node", "dr", "striker") + { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { host_type => $host_type }}); + foreach my $short_host_name (sort {$a cmp $b} keys %{$anvil->data->{trusted_host}{$host_type}}) + { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { short_host_name => $short_host_name }}); + foreach my $network_type ("bcn", "sn", "ifn") + { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { short_host_name => $short_host_name }}); + foreach my $sequence (sort {$a cmp $b} keys %{$anvil->data->{trusted_host}{$host_type}{$short_host_name}{network}{$network_type}}) + { + my $ip_address = $anvil->data->{trusted_host}{$host_type}{$short_host_name}{network}{$network_type}{$sequence}{ip_address}; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { ip_address => $ip_address }}); + + # Make sure this host is in the 'hosts::needed::xxx::ip_address' + # hash. If we saw it already and it was OK, it was deleted from + # there. + if (not exists $anvil->data->{hosts}{needed}{$short_host_name}) + { + ### NOTE: We're possibly missing short and full hostnames if + ### this is BCN1, but they should all change at once. + ### Maybe need to rethink this later. + # Already seen. + next; + } + + # Start the line for this IP. + if (not exists $lines->{$ip_address}) + { + $lines->{$ip_address} = $ip_address."\t"; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "lines->${ip_address}" => $lines->{$ip_address} }}); + + # Push the IP into the array so that we print them in the + # order be first saw them. + push @{$ip_order}, $ip_address; + } + $lines->{$ip_address} .= $short_host_name.".".$network_type.$sequence." "; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + "lines->${ip_address}" => $lines->{$ip_address}, + }}); + } + } + } + } + + my $new_line_count = \@{$ip_order}; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { new_line_count => $new_line_count }}); + if ($new_line_count) + { + $changes = 1; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { changes => $changes }}); + + foreach my $ip_address (@{$ip_order}) + { + $new_body .= $lines->{$ip_address}."\n"; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { new_body => $new_body }}); + } + } + + if ($changes) + { + # Write the new file. + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { new_body => $new_body }}); + die; + } return(0); } diff --git a/Anvil/Tools/Validate.pm b/Anvil/Tools/Validate.pm index 9d4cca68..4642bb5d 100644 --- a/Anvil/Tools/Validate.pm +++ b/Anvil/Tools/Validate.pm @@ -7,6 +7,7 @@ use strict; use warnings; use Data::Dumper; use Data::Validate::Domain qw(is_domain); +use Data::Validate::IP; use Scalar::Util qw(weaken isweak); use Mail::RFC822::Address qw(valid validlist); @@ -14,17 +15,19 @@ our $VERSION = "3.0.0"; my $THIS_FILE = "Validate.pm"; ### Methods; +# alphanumeric +# domain_name +# email # form_field -# is_alphanumeric -# is_domain_name -# is_email -# is_hex -# is_host_name -# is_ipv4 -# is_mac -# is_positive_integer -# is_subnet_mask -# is_uuid +# hex +# host_name +# ip +# ipv4 +# ipv6 +# mac +# positive_integer +# subnet_mask +# uuid =pod @@ -45,8 +48,8 @@ Provides all methods related to data validation. # Access to methods using '$anvil->Validate->X'. # - # Example using 'is_uuid()'; - if ($anvil->Validate->is_uuid({uuid => $string})) + # Example using 'uuid()'; + if ($anvil->Validate->uuid({uuid => $string})) { print "The UUID: [$string] is valid!\n"; } @@ -89,6 +92,108 @@ sub parent # Public methods # ############################################################################################################# +=head2 alphanumeric + +This verifies that the passed-in string contains only alpha-numeric characters. This is strict and will return invalid if spaces, hyphens or other characters are found. + +NOTE: An empty string is considered invalid. + + $string = "4words"; + if ($anvil->Validate->alphanumeric({string => $string})) + { + print "The string: [$string] is valid!\n"; + } + +Parameters; + +=head3 string (required) + +This is the string name to validate. + +=cut +sub alphanumeric +{ + my $self = shift; + my $parameter = shift; + my $anvil = $self->parent; + my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; + + my $valid = 1; + my $string = defined $parameter->{string} ? $parameter->{string} : ""; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { string => $string }}); + + if (not $string) + { + $valid = 0; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); + } + + if ($string !~ /^[a-zA-Z0-9]+$/) + { + $valid = 0; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); + } + + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); + return($valid); +} + +=head2 domain_name + +Checks if the passed-in string is a valid domain name. Returns 'C<< 1 >>' if OK, 'C<< 0 >>' if not. + + $name = "alteeve.com"; + if ($anvil->Validate->domain_name({name => $name})) + { + print "The domain name: [$name] is valid!\n"; + } + +Parameters; + +=head3 name (required) + +This is the domain name to validate. + +=cut +sub domain_name +{ + my $self = shift; + my $parameter = shift; + my $anvil = $self->parent; + my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; + + my $valid = 1; + my $name = $parameter->{name} ? $parameter->{name} : ""; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { name => $name }}); + + if (not $name) + { + $valid = 0; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); + } + else + { + # Underscores are allowd in domain names, but not host names. We disable TLD checks as we + # frequently use '.remote', '.bcn', etc. + ### TODO: Add a 'strict' parameter to control this) and/or support domain_private_tld + my %options = (domain_allow_underscore => 1, domain_disable_tld_validation => 1); + my $dvd = Data::Validate::Domain->new(%options); + my $test = $dvd->domain($name); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { test => $test }}); + if (not $test) + { + # Doesn't appear to be valid. + $valid = 0; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); + } + } + + + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); + return($valid); +} + + =head2 form_field This validates that a given HTML form field is valid. It takes an input ID and the type of data that is expected. If it is sane, C<< 1 >> is returned. If it fails to validate, C<< 0 >> is returned and C<< cgi::::alert >> is set to C<< 1 >>. @@ -173,49 +278,49 @@ sub form_field $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); } } - elsif (($type eq "alphanumeric") && (not $anvil->Validate->is_alphanumeric({string => $anvil->data->{cgi}{$name}{value}}))) + elsif (($type eq "alphanumeric") && (not $anvil->Validate->alphanumeric({string => $anvil->data->{cgi}{$name}{value}}))) { $valid = 0; $anvil->data->{cgi}{$name}{alert} = 1; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 1, list => { valid => $valid, "cgi::${name}::alert" => $anvil->data->{cgi}{$name}{alert} }}); } - elsif (($type eq "domain_name") && (not $anvil->Validate->is_domain_name({name => $anvil->data->{cgi}{$name}{value}}))) + elsif (($type eq "domain_name") && (not $anvil->Validate->domain_name({name => $anvil->data->{cgi}{$name}{value}}))) { $valid = 0; $anvil->data->{cgi}{$name}{alert} = 1; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 1, list => { valid => $valid, "cgi::${name}::alert" => $anvil->data->{cgi}{$name}{alert} }}); } - elsif (($type eq "email") && (not $anvil->Validate->is_email({email => $anvil->data->{cgi}{$name}{value}}))) + elsif (($type eq "email") && (not $anvil->Validate->email({email => $anvil->data->{cgi}{$name}{value}}))) { $valid = 0; $anvil->data->{cgi}{$name}{alert} = 1; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 1, list => { valid => $valid, "cgi::${name}::alert" => $anvil->data->{cgi}{$name}{alert} }}); } - elsif (($type eq "ipv4") && (not $anvil->Validate->is_ipv4({ip => $anvil->data->{cgi}{$name}{value}}))) + elsif (($type eq "ipv4") && (not $anvil->Validate->ipv4({ip => $anvil->data->{cgi}{$name}{value}}))) { $valid = 0; $anvil->data->{cgi}{$name}{alert} = 1; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 1, list => { valid => $valid, "cgi::${name}::alert" => $anvil->data->{cgi}{$name}{alert} }}); } - elsif (($type eq "mac") && (not $anvil->Validate->is_mac({mac => $anvil->data->{cgi}{$name}{value}}))) + elsif (($type eq "mac") && (not $anvil->Validate->mac({mac => $anvil->data->{cgi}{$name}{value}}))) { $valid = 0; $anvil->data->{cgi}{$name}{alert} = 1; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 1, list => { valid => $valid, "cgi::${name}::alert" => $anvil->data->{cgi}{$name}{alert} }}); } - elsif (($type eq "positive_integer") && (not $anvil->Validate->is_positive_integer({number => $anvil->data->{cgi}{$name}{value}, zero => $zero}))) + elsif (($type eq "positive_integer") && (not $anvil->Validate->positive_integer({number => $anvil->data->{cgi}{$name}{value}, zero => $zero}))) { $valid = 0; $anvil->data->{cgi}{$name}{alert} = 1; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 1, list => { valid => $valid, "cgi::${name}::alert" => $anvil->data->{cgi}{$name}{alert} }}); } - elsif (($type eq "subnet_mask") && (not $anvil->Validate->is_subnet_mask({subnet_mask => $anvil->data->{cgi}{$name}{value}}))) + elsif (($type eq "subnet_mask") && (not $anvil->Validate->subnet_mask({subnet_mask => $anvil->data->{cgi}{$name}{value}}))) { $valid = 0; $anvil->data->{cgi}{$name}{alert} = 1; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 1, list => { valid => $valid, "cgi::${name}::alert" => $anvil->data->{cgi}{$name}{alert} }}); } - elsif (($type eq "uuid") && (not $anvil->Validate->is_uuid({uuid => $anvil->data->{cgi}{$name}{value}}))) + elsif (($type eq "uuid") && (not $anvil->Validate->uuid({uuid => $anvil->data->{cgi}{$name}{value}}))) { $valid = 0; $anvil->data->{cgi}{$name}{alert} = 1; @@ -228,109 +333,8 @@ sub form_field return($valid); } -=head2 is_alphanumeric - -This verifies that the passed-in string contains only alpha-numeric characters. This is strict and will return invalid if spaces, hyphens or other characters are found. - -NOTE: An empty string is considered invalid. - - $string = "4words"; - if ($anvil->Validate->is_alphanumeric({string => $string})) - { - print "The string: [$string] is valid!\n"; - } - -Parameters; - -=head3 string (required) -This is the string name to validate. - -=cut -sub is_alphanumeric -{ - my $self = shift; - my $parameter = shift; - my $anvil = $self->parent; - my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; - - my $valid = 1; - my $string = defined $parameter->{string} ? $parameter->{string} : ""; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { string => $string }}); - - if (not $string) - { - $valid = 0; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); - } - - if ($string !~ /^[a-zA-Z0-9]+$/) - { - $valid = 0; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); - } - - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); - return($valid); -} - -=head2 is_domain_name - -Checks if the passed-in string is a valid domain name. Returns 'C<< 1 >>' if OK, 'C<< 0 >>' if not. - - $name = "alteeve.com"; - if ($anvil->Validate->is_domain_name({name => $name})) - { - print "The domain name: [$name] is valid!\n"; - } - -Parameters; - -=head3 name (required) - -This is the domain name to validate. - -=cut -sub is_domain_name -{ - my $self = shift; - my $parameter = shift; - my $anvil = $self->parent; - my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; - - my $valid = 1; - my $name = $parameter->{name} ? $parameter->{name} : ""; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { name => $name }}); - - if (not $name) - { - $valid = 0; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); - } - else - { - # Underscores are allowd in domain names, but not host names. We disable TLD checks as we - # frequently use '.remote', '.bcn', etc. - ### TODO: Add a 'strict' parameter to control this) and/or support domain_private_tld - my %options = (domain_allow_underscore => 1, domain_disable_tld_validation => 1); - my $dvd = Data::Validate::Domain->new(%options); - my $test = $dvd->is_domain($name); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { test => $test }}); - if (not $test) - { - # Doesn't appear to be valid. - $valid = 0; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); - } - } - - - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); - return($valid); -} - - -=head2 is_hex +=head2 hex Checks if the passed-in string contains only hexidecimal characters. A prefix of C<< 0x >> is allowed. @@ -345,7 +349,7 @@ If set to C<< 1 >>, the string will be allowed to contain C<< : >> and C<< - >> This is the string to validate =cut -sub is_hex +sub hex { my $self = shift; my $parameter = shift; @@ -380,14 +384,14 @@ sub is_hex } -=head2 is_host_name +=head2 host_name Checks if the passed-in string is a valid host name. Returns 'C<< 1 >>' if OK, 'C<< 0 >>' if not. -B: If this method receives a full domain name, the host name is checked in this method and the domain (anything after the first C<< . >>) is tested using C<< Validate->is_domain_name >>. If either fails, C<< 0 >> is returned. +B: If this method receives a full domain name, the host name is checked in this method and the domain (anything after the first C<< . >>) is tested using C<< Validate->domain_name >>. If either fails, C<< 0 >> is returned. $name = "an-a05n01"; - if ($anvil->Validate->is_host_name({name => $name})) + if ($anvil->Validate->host_name({name => $name})) { print "The host name: [$name] is valid!\n"; } @@ -399,7 +403,7 @@ Parameters; This is the host name to validate. =cut -sub is_host_name +sub host_name { my $self = shift; my $parameter = shift; @@ -422,7 +426,7 @@ sub is_host_name if ($domain) { - $valid = $anvil->Validate->is_domain_name({ + $valid = $anvil->Validate->domain_name({ name => $domain, debug => $debug, }); @@ -439,7 +443,7 @@ sub is_host_name # Underscores are allowd in domain names, but not host names. my %options = (domain_allow_underscore => 1); my $dvd = Data::Validate::Domain->new(%options); - my $test = $dvd->is_hostname($name); + my $test = $dvd->hostname($name); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { test => $test }}); if (not $test) { @@ -454,12 +458,12 @@ sub is_host_name } -=head2 is_email +=head2 email Checks if the passed-in string is a valid address. Returns 'C<< 1 >>' if OK, 'C<< 0 >>' if not. $email = "test@example.com"; - if ($anvil->Validate->is_email({email => $email})) + if ($anvil->Validate->email({email => $email})) { print "The email address: [$email] is valid!\n"; } @@ -471,7 +475,7 @@ Parameters; This is the email address to verify. =cut -sub is_email +sub email { my $self = shift; my $parameter = shift; @@ -493,12 +497,53 @@ sub is_email return($valid); } -=head2 is_ipv4 +=head2 ip + +This is a meta method. It takes the IP and tests it against both C<< ipv4 >> and C<< ipv6 >>. If either return as valid, this method returns as valid. + +Said more simply; This tests an IP to see if it is IPv4 OR IPv6. Returns 'C<< 1 >>' if OK, 'C<< 0 >>' if not. + +Parameters; + +=head3 ip (required) + +This is the IP address to validate. + +=cut +sub ip +{ + my $self = shift; + my $parameter = shift; + my $anvil = $self->parent; + my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; + + my $ip = defined $parameter->{ip} ? $parameter->{ip} : ""; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { ip => $ip }}); + + + my $ipv4 = $anvil->Validate->ipv4({ip => $ip, debug => $debug}); + my $ipv6 = not $ipv4 ? $anvil->Validate->ipv6({ip => $ip, debug => $debug}) : 0; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + ipv4 => $ipv4, + ipv6 => $ipv6, + }}); + + my $valid = 1; + if ((not $ipv4) && (not $ipv6)) + { + $valid = 0; + } + + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); + return($valid); +} + +=head2 ipv4 Checks if the passed-in string is an IPv4 address. Returns 'C<< 1 >>' if OK, 'C<< 0 >>' if not. $ip = "111.222.33.44"; - if ($anvil->Validate->is_ipv4({ip => $ip})) + if ($anvil->Validate->ipv4({ip => $ip})) { print "The IP address: [$ip] is valid!\n"; } @@ -510,7 +555,7 @@ Parameters; This is the IP address to verify. =cut -sub is_ipv4 +sub ipv4 { my $self = shift; my $parameter = shift; @@ -521,33 +566,46 @@ sub is_ipv4 $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { ip => $ip }}); my $valid = 1; - if ($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) + if (not is_ipv4($ip)) { - # It is in the right format. - my $first_octet = $1; - my $second_octet = $2; - my $third_octet = $3; - my $fourth_octet = $4; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { - first_octet => $first_octet, - second_octet => $second_octet, - third_octet => $third_octet, - fourth_octet => $fourth_octet, - }}); - - if (($first_octet < 0) or ($first_octet > 255) or - ($second_octet < 0) or ($second_octet > 255) or - ($third_octet < 0) or ($third_octet > 255) or - ($fourth_octet < 0) or ($fourth_octet > 255)) - { - # One of the octets is out of range. - $valid = 0; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); - } + $valid = 0; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); } - else + + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); + return($valid); +} + +=head2 ipv6 + +Checks if the passed-in string is an IPv6 address. Returns 'C<< 1 >>' if OK, 'C<< 0 >>' if not. + + $ip = "::1"; + if ($anvil->Validate->ipv6({ip => $ip})) + { + print "The IP address: [$ip] is valid!\n"; + } + +Parameters; + +=head3 ip (required) + +This is the IPv6 address to verify. + +=cut +sub ipv6 +{ + my $self = shift; + my $parameter = shift; + my $anvil = $self->parent; + my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; + + my $ip = defined $parameter->{ip} ? $parameter->{ip} : ""; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { ip => $ip }}); + + my $valid = 1; + if (not is_ipv6($ip)) { - # Not in the right format. $valid = 0; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { valid => $valid }}); } @@ -556,7 +614,7 @@ sub is_ipv4 return($valid); } -=head2 is_mac +=head2 mac Checks if the passed-in string is a valid network MAC address. Returns 'C<< 1 >>' if OK, 'C<< 0 >>' if not. @@ -567,7 +625,7 @@ Parameters; This is the network MAC address to verify. =cut -sub is_mac +sub mac { my $self = shift; my $parameter = shift; @@ -589,7 +647,7 @@ sub is_mac return($valid); } -=head2 is_port +=head2 port This tests to see if the value passed is a valid TCP/UDP port (1 ~ 65536). Returns 'C<< 1 >>' if OK, 'C<< 0 >>' if not. @@ -602,7 +660,7 @@ Parameters; This is the port being tested. =cut -sub is_port +sub port { my $self = shift; my $parameter = shift; @@ -630,14 +688,14 @@ sub is_port return($valid); } -=head2 is_positive_integer +=head2 positive_integer This method verifies that the passed in value is a positive integer. NOTE: This method is strict and will only validate numbers without decimal places and that have no sign or a positive sign only (ie: C<< +3 >>, or C<< 3 >> are valid, but C<< -3 >> or C<< 3.0 >> are not). my $number = 3; - if ($anvil->Validate->is_positive_integer({number => $number})) + if ($anvil->Validate->positive_integer({number => $number})) { print "The number: [$number] is valid!\n"; } @@ -653,7 +711,7 @@ This is the number to verify. If set, the number C<< 0 >> will be considered valid. By default, c<< 0 >> is not considered "positive". =cut -sub is_positive_integer +sub positive_integer { my $self = shift; my $parameter = shift; @@ -688,7 +746,7 @@ sub is_positive_integer return($valid); } -=head2 is_subnet_mask +=head2 subnet_mask This method takes a subnet mask string and checks to see if it is a valid IPv4 address or CIDR notation. It returns 'C<< 1 >>' if it is a valid address. Otherwise it returns 'C<< 0 >>'. @@ -699,7 +757,7 @@ Parameters; This is the address to verify. =cut -sub is_subnet_mask +sub subnet_mask { my $self = shift; my $parameter = shift; @@ -715,7 +773,7 @@ sub is_subnet_mask if ($subnet_mask) { # We have something. Is it an IPv4 address? - if ($anvil->Validate->is_ipv4({ip => $subnet_mask})) + if ($anvil->Validate->ipv4({ip => $subnet_mask})) { # It is. Try converting it to a CIDR notation. If we get an empty string back, it isn't valid. my $cidr = $anvil->Convert->cidr({subnet_mask => $subnet_mask}); @@ -745,13 +803,13 @@ sub is_subnet_mask return($valid); } -=head2 is_uuid +=head2 uuid This method takes a UUID string and returns 'C<< 1 >>' if it is a valid UUID string. Otherwise it returns 'C<< 0 >>'. NOTE: This method is strict and will only validate UUIDs that are lower case! - if ($anvil->Validate->is_uuid({uuid => $string})) + if ($anvil->Validate->uuid({uuid => $string})) { print "The UUID: [$string] is valid!\n"; } @@ -763,7 +821,7 @@ Parameters; This is the UUID to verify. =cut -sub is_uuid +sub uuid { my $self = shift; my $parameter = shift; diff --git a/cgi-bin/striker b/cgi-bin/striker index 5ccac5c1..b90b3984 100755 --- a/cgi-bin/striker +++ b/cgi-bin/striker @@ -551,7 +551,7 @@ WHERE } # Verify that the mail server and ports are sane. - if (not $anvil->Validate->is_email({email => $recipient_email})) + if (not $anvil->Validate->email({email => $recipient_email})) { # Bad domain my $error_message = $anvil->Words->string({key => "warning_0026"}); @@ -946,8 +946,8 @@ WHERE } # Verify that the mail server and ports are sane. - if ((not $anvil->Validate->is_ipv4({ip => $test_outgoing_mail_server})) and - (not $anvil->Validate->is_domain_name({name => $test_outgoing_mail_server}))) + if ((not $anvil->Validate->ipv4({ip => $test_outgoing_mail_server})) and + (not $anvil->Validate->domain_name({name => $test_outgoing_mail_server}))) { # Bad domain my $error_message = $anvil->Words->string({key => "warning_0023"}); @@ -963,7 +963,7 @@ WHERE confirm => $confirm, }}); } - if (not $anvil->Validate->is_port({port => $port})) + if (not $anvil->Validate->port({port => $port})) { # Bad port my $error_message = $anvil->Words->string({key => "warning_0024"}); @@ -1552,7 +1552,7 @@ sub process_manifests }}); # Are we running a manifest, or creating/editing one? - if (($anvil->data->{cgi}{run}{value}) && ($anvil->Validate->is_uuid({uuid => $anvil->data->{cgi}{manifest_uuid}{value}}))) + if (($anvil->data->{cgi}{run}{value}) && ($anvil->Validate->uuid({uuid => $anvil->data->{cgi}{manifest_uuid}{value}}))) { run_manifest($anvil); } @@ -2947,7 +2947,7 @@ sub sanity_check_manifest_step3 if ($anvil->data->{cgi}{$key}{value}) { # Is it a valid IPv4 address? - if (not $anvil->Validate->is_ipv4({ip => $anvil->data->{cgi}{$key}{value}})) + if (not $anvil->Validate->ipv4({ip => $anvil->data->{cgi}{$key}{value}})) { # Bad subnet my $say_network = "#!string!striker_0255!#"; @@ -3042,7 +3042,7 @@ sub sanity_check_manifest_step3 }}); # Is the IP valid? - if (not $anvil->Validate->is_ipv4({ip => $anvil->data->{cgi}{$machine_ip_key}{value}, debug => 2})) + if (not $anvil->Validate->ipv4({ip => $anvil->data->{cgi}{$machine_ip_key}{value}, debug => 2})) { # Bad subnet my $say_network = "#!string!striker_0255!#"; @@ -3181,7 +3181,7 @@ sub sanity_check_manifest_step2 $ip =~ s/\s+$//; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { ip => $ip }}); - if (not $anvil->Validate->is_ipv4({ip => $ip})) + if (not $anvil->Validate->ipv4({ip => $ip})) { # Bad network my $message = $anvil->Words->string({key => "error_0015"}); @@ -3208,7 +3208,7 @@ sub sanity_check_manifest_step2 $ip =~ s/\s+$//; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { ip => $ip }}); - if (not $anvil->Validate->is_ipv4({ip => $ip})) + if (not $anvil->Validate->ipv4({ip => $ip})) { # Bad network my $message = $anvil->Words->string({key => "error_0122"}); @@ -3226,7 +3226,7 @@ sub sanity_check_manifest_step2 } # Make sure the MTU is sane - if (not $anvil->Validate->is_positive_integer({number => $anvil->data->{cgi}{mtu}{value}})) + if (not $anvil->Validate->positive_integer({number => $anvil->data->{cgi}{mtu}{value}})) { my $message = $anvil->Words->string({key => "error_0123"}); $anvil->data->{form}{error_massage} = $anvil->Template->get({file => "main.html", name => "error_message", variables => { error_message => $message }}); @@ -3249,7 +3249,7 @@ sub check_network # Make sure the network and subnet are valid my $local_sane = 1; - if (not $anvil->Validate->is_ipv4({ip => $anvil->data->{cgi}{$network_key}{value}})) + if (not $anvil->Validate->ipv4({ip => $anvil->data->{cgi}{$network_key}{value}})) { # Bad network my $message = $anvil->Words->string({key => "error_0020", variables => { field => $say_network." - #!string!striker_0149!#" }}); @@ -3264,7 +3264,7 @@ sub check_network "cgi::${network_key}::value" => $anvil->data->{cgi}{$network_key}{value}, }}); } - if (not $anvil->Validate->is_ipv4({ip => $anvil->data->{cgi}{$subnet_key}{value}})) + if (not $anvil->Validate->ipv4({ip => $anvil->data->{cgi}{$subnet_key}{value}})) { # Bad subnet my $message = $anvil->Words->string({key => "error_0020", variables => { field => $say_network." - #!string!striker_0025!#" }}); @@ -3281,7 +3281,7 @@ sub check_network } # The gateway can be blank - if (($anvil->data->{cgi}{$gateway_key}{value}) && (not $anvil->Validate->is_ipv4({ip => $anvil->data->{cgi}{$gateway_key}{value}}))) + if (($anvil->data->{cgi}{$gateway_key}{value}) && (not $anvil->Validate->ipv4({ip => $anvil->data->{cgi}{$gateway_key}{value}}))) { # It's not a valid IP. my $message = $anvil->Words->string({key => "error_0118", variables => { ip => $anvil->data->{cgi}{$gateway_key}{value} }}); @@ -3376,7 +3376,7 @@ sub sanity_check_manifest_step1 my ($anvil) = @_; my $sane = 1; - if ((not $anvil->Validate->is_alphanumeric({string => $anvil->data->{cgi}{prefix}{value}})) or (length($anvil->data->{cgi}{prefix}{value}) > 5)) + if ((not $anvil->Validate->alphanumeric({string => $anvil->data->{cgi}{prefix}{value}})) or (length($anvil->data->{cgi}{prefix}{value}) > 5)) { my $message = $anvil->Words->string({key => "error_0020", variables => { field => "#!string!striker_0228!#" }}); if ($anvil->data->{cgi}{prefix}{value}) @@ -3394,7 +3394,7 @@ sub sanity_check_manifest_step1 } # We can use Validate to check the domain. - if (not $anvil->Validate->is_domain_name({name => $anvil->data->{cgi}{domain}{value}, debug => 2})) + if (not $anvil->Validate->domain_name({name => $anvil->data->{cgi}{domain}{value}, debug => 2})) { my $message = $anvil->Words->string({key => "error_0020", variables => { field => "#!string!striker_0007!#" }}); if ($anvil->data->{cgi}{domain}{value}) @@ -3412,7 +3412,7 @@ sub sanity_check_manifest_step1 } # The sequence and IFN count need to be integers. - if (not $anvil->Validate->is_positive_integer({number => $anvil->data->{cgi}{sequence}{value}})) + if (not $anvil->Validate->positive_integer({number => $anvil->data->{cgi}{sequence}{value}})) { my $message = $anvil->Words->string({key => "error_0020", variables => { field => "#!string!striker_0009!#" }}); if ($anvil->data->{cgi}{sequence}{value}) @@ -3428,7 +3428,7 @@ sub sanity_check_manifest_step1 "cgi::sequence::value" => $anvil->data->{cgi}{sequence}{value}, }}); } - if (not $anvil->Validate->is_positive_integer({number => $anvil->data->{cgi}{ifn_count}{value}})) + if (not $anvil->Validate->positive_integer({number => $anvil->data->{cgi}{ifn_count}{value}})) { my $message = $anvil->Words->string({key => "error_0020", variables => { field => "#!string!striker_0230!#" }}); if ($anvil->data->{cgi}{ifn_count}{value}) @@ -3736,7 +3736,7 @@ sub process_upses }}); # Are any values insane? - if (not $anvil->Validate->is_host_name({name => $anvil->data->{cgi}{$ups_name_key}{value}, debug => 2})) + if (not $anvil->Validate->host_name({name => $anvil->data->{cgi}{$ups_name_key}{value}, debug => 2})) { # Bad host name $sane = 0; @@ -3746,7 +3746,7 @@ sub process_upses sane => $sane, }}); } - if (not $anvil->Validate->is_ipv4({ip => $anvil->data->{cgi}{$ups_ip_address_key}{value}})) + if (not $anvil->Validate->ipv4({ip => $anvil->data->{cgi}{$ups_ip_address_key}{value}})) { # Bad IP address. $sane = 0; @@ -4998,7 +4998,7 @@ sub process_prep_network if ($anvil->data->{cgi}{host_name}{value}) { # Is the host name sane? - if (not $anvil->Validate->is_domain_name({name => $anvil->data->{cgi}{host_name}{value}})) + if (not $anvil->Validate->domain_name({name => $anvil->data->{cgi}{host_name}{value}})) { # Nope my $error_message = $anvil->Words->string({key => "error_0012", variables => { host_name => $anvil->data->{cgi}{host_name}{value} }}); @@ -5022,7 +5022,7 @@ sub process_prep_network if ($anvil->data->{cgi}{gateway}{value}) { # Is if valid? - if (not $anvil->Validate->is_ipv4({ip => $anvil->data->{cgi}{gateway}{value}})) + if (not $anvil->Validate->ipv4({ip => $anvil->data->{cgi}{gateway}{value}})) { # Bad IP my $error_message = $anvil->Words->string({key => "warning_0010", variables => { ip_address => $anvil->data->{cgi}{gateway}{value} }}); @@ -5050,7 +5050,7 @@ sub process_prep_network { $dns =~ s/^\s+//; $dns =~ s/\s+$//; - if (not $anvil->Validate->is_ipv4({ip => $dns})) + if (not $anvil->Validate->ipv4({ip => $dns})) { # Bad IP $anvil->data->{form}{error_massage} = $anvil->Template->get({file => "main.html", name => "error_message", variables => { error_message => $anvil->Words->string({key => "warning_0010", variables => { ip_address => $anvil->data->{cgi}{dns}{value} }}) }}); @@ -5136,7 +5136,7 @@ sub process_prep_network } my $say_ip_address = "#!string!striker_0152!#"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { say_ip_address => $say_ip_address }}); - if (($anvil->data->{cgi}{$ip_key}{value}) && (not $anvil->Validate->is_ipv4({ip => $anvil->data->{cgi}{$ip_key}{value}}))) + if (($anvil->data->{cgi}{$ip_key}{value}) && (not $anvil->Validate->ipv4({ip => $anvil->data->{cgi}{$ip_key}{value}}))) { # Bad IP $anvil->data->{form}{error_massage} = $anvil->Template->get({file => "main.html", name => "error_message", variables => { error_message => $anvil->Words->string({key => "warning_0010", variables => { ip_address => $anvil->data->{cgi}{$ip_key}{value} }}) }}); @@ -5146,7 +5146,7 @@ sub process_prep_network "cgi::${ip_key}::alert" => $anvil->data->{cgi}{$ip_key}{alert}, }}); } - elsif (($anvil->data->{cgi}{$subnet_key}{value}) && (not $anvil->Validate->is_subnet_mask({subnet_mask => $anvil->data->{cgi}{$subnet_key}{value}}))) + elsif (($anvil->data->{cgi}{$subnet_key}{value}) && (not $anvil->Validate->subnet_mask({subnet_mask => $anvil->data->{cgi}{$subnet_key}{value}}))) { # Bad subnet $anvil->data->{form}{error_massage} = $anvil->Template->get({file => "main.html", name => "error_message", variables => { error_message => $anvil->Words->string({key => "warning_0017"}) }}); @@ -5706,7 +5706,7 @@ sub process_prep_host_page } elsif ($connect) { - if (not $anvil->Validate->is_ipv4({ip => $host_ip_address})) + if (not $anvil->Validate->ipv4({ip => $host_ip_address})) { # Bad IP $anvil->data->{form}{error_massage} = $anvil->Template->get({file => "main.html", name => "error_message", variables => { error_message => $anvil->Words->string({key => "warning_0010", variables => { ip_address => $host_ip_address }}) }}); @@ -6162,8 +6162,8 @@ sub process_sync_page foreach my $interface (sort {$a cmp $b} keys %{$anvil->data->{network}{'local'}{interface}}) { next if (($interface !~ /^bcn/) && ($interface !~ /^ifn/)); - next if not $anvil->Validate->is_ipv4({ip => $anvil->data->{network}{'local'}{interface}{$interface}{ip}}); - next if not $anvil->Validate->is_subnet_mask({subnet_mask => $anvil->data->{network}{'local'}{interface}{$interface}{subnet_mask}}); + next if not $anvil->Validate->ipv4({ip => $anvil->data->{network}{'local'}{interface}{$interface}{ip}}); + next if not $anvil->Validate->subnet_mask({subnet_mask => $anvil->data->{network}{'local'}{interface}{$interface}{subnet_mask}}); my ($network_type, $network_number) = ($interface =~ /^(.*?)(\d+)_/); my $database_user = $anvil->data->{database}{$host_uuid}{user} ? $anvil->data->{database}{$host_uuid}{user} : $anvil->data->{sys}{database}{user}; @@ -6197,7 +6197,7 @@ sub process_sync_page { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { uuid => $uuid }}); - next if not $anvil->Validate->is_uuid({uuid => $uuid}); + next if not $anvil->Validate->uuid({uuid => $uuid}); next if $uuid eq $host_uuid; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { uuid => $uuid }}); @@ -6402,8 +6402,8 @@ sub add_sync_peer # Is the host a domain or IP? # If so, and 'bi-directional' is set, verify we can ssh into the peer. - my $is_domain = $anvil->Validate->is_domain_name({name => $host}); - my $is_ipv4 = $anvil->Validate->is_ipv4({ip => $host}); + my $is_domain = $anvil->Validate->domain_name({name => $host}); + my $is_ipv4 = $anvil->Validate->ipv4({ip => $host}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { is_domain => $is_domain, is_ipv4 => $is_ipv4, @@ -7375,7 +7375,7 @@ sub sanity_check_step2 $ip =~ s/^\s+//; $ip =~ s/\s+$//; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { ip => $ip }}); - if (not $anvil->Validate->is_ipv4({ip => $ip})) + if (not $anvil->Validate->ipv4({ip => $ip})) { $anvil->data->{form}{error_massage} = $anvil->Template->get({file => "main.html", name => "error_message", variables => { error_message => $anvil->Words->string({key => "error_0015"}) }}); $anvil->data->{cgi}{dns}{alert} = 1; @@ -7701,7 +7701,7 @@ sub sanity_check_step1 } # The prefix needs to be alphanumeric and be between 1 ~ 5 chatacters. - if ((not $anvil->Validate->is_alphanumeric({string => $anvil->data->{cgi}{prefix}{value}})) or (length($anvil->data->{cgi}{prefix}{value}) > 5)) + if ((not $anvil->Validate->alphanumeric({string => $anvil->data->{cgi}{prefix}{value}})) or (length($anvil->data->{cgi}{prefix}{value}) > 5)) { $anvil->data->{form}{error_massage} = $anvil->Template->get({file => "main.html", name => "error_message", variables => { error_message => $anvil->Words->string({key => "error_0021"}) }}); $anvil->data->{cgi}{prefix}{alert} = 1; @@ -7747,7 +7747,7 @@ sub sanity_check_step1 } # The sequence and IFN count need to be integers. - if (not $anvil->Validate->is_positive_integer({number => $anvil->data->{cgi}{sequence}{value}})) + if (not $anvil->Validate->positive_integer({number => $anvil->data->{cgi}{sequence}{value}})) { $anvil->data->{form}{error_massage} = $anvil->Template->get({file => "main.html", name => "error_message", variables => { error_message => $anvil->Words->string({key => "error_0022", variables => { field => "striker_0009" }}) }}); $anvil->data->{cgi}{sequence}{alert} = 1; @@ -7769,7 +7769,7 @@ sub sanity_check_step1 }); } - if (not $anvil->Validate->is_positive_integer({number => $anvil->data->{cgi}{ifn_count}{value}})) + if (not $anvil->Validate->positive_integer({number => $anvil->data->{cgi}{ifn_count}{value}})) { $anvil->data->{form}{error_massage} = $anvil->Template->get({file => "main.html", name => "error_message", variables => { error_message => $anvil->Words->string({key => "error_0022", variables => { field => "striker_0011" }}) }}); $anvil->data->{cgi}{ifn_count}{alert} = 1; @@ -8237,7 +8237,7 @@ sub generate_ip default_subnet_mask => $default_subnet_mask, }}); - if (($anvil->Validate->is_ipv4({ip => $default_ip})) && ($anvil->Validate->is_ipv4({ip => $default_subnet_mask}))) + if (($anvil->Validate->ipv4({ip => $default_ip})) && ($anvil->Validate->ipv4({ip => $default_subnet_mask}))) { # Valid values. my ($ip_octet1, $ip_octet2, $ip_octet3, $ip_octet4) = (split/\./, $default_ip); diff --git a/rpm/SPECS/anvil.spec b/rpm/SPECS/anvil.spec index 5c4b5e5a..b0a81187 100644 --- a/rpm/SPECS/anvil.spec +++ b/rpm/SPECS/anvil.spec @@ -42,6 +42,7 @@ Requires: mlocate Requires: perl-Capture-Tiny Requires: perl-Data-Dumper Requires: perl-Data-Validate-Domain +Requires: perl-Data-Validate-IP Requires: perl-DBD-Pg Requires: perl-DBI Requires: perl-Data-Validate-Domain diff --git a/share/words.xml b/share/words.xml index 42b43ae9..85ee30e5 100644 --- a/share/words.xml +++ b/share/words.xml @@ -897,6 +897,7 @@ If the targets are unique, did you copy the full database directory? A unique id The libvirtd' daemon isn't running. Will check for and remove virsh networks set to start on boot. Removing the symlink: [#!variable!symlink!#]. Updating the cache state file. + [ Note ] - The host: [#!variable!host!#] entry in /etc/hosts has changed IP from: [#!variable!old_ip!#] to: [#!variable!new_ip!#]. The host name: [#!variable!target!#] does not resolve to an IP address. @@ -1155,6 +1156,16 @@ About to try to download aproximately: [#!variable!packages!#] packages needed t Confirmed by switch, proceeding with purge of: [#!variable!host_name!#]. Thank you, proceeding. The host: [#!variable!host_name!#] has been purged. + +##] anvil-daemon [########################################################################################### +# NOTE: The /etc/hosts file is managed by the Anvil! system. Manual additions will be retained, but # +# conflicts with hosts managed by the Anvil! system will be overwritten. Specifically, all hosts # +# related to Striker dashboards and, for hosts in an Anvil!, peer nodes and DR hosts will be set to # +# use the IPs recorded in the Anvil! database (which themselves are recorded by the anvil-daemon # +# running on each host). If / when an IP address changes, the host files on all associated hosts # +# should update within a minute. # +############################################################################################################# + Saved the mail server information successfully! @@ -1632,6 +1643,7 @@ Here we will inject 't_0006', which injects 't_0001' which has a variable: [#!st [ Warning ] - The password verification was not set. [ Warning ] - The passwords do not match. [ Warning ] - The host: [#!variable!host!#] now belongs to the #!string!brand_0006!#, it can't be used here anymore. + [ Warning ] - The IP address: [#!variable!ip!#] is not valid. Ignoring associated hosts: [#!variable!hosts!#]. diff --git a/tools/anvil-configure-host b/tools/anvil-configure-host index a6360e06..1a362896 100755 --- a/tools/anvil-configure-host +++ b/tools/anvil-configure-host @@ -398,7 +398,7 @@ sub reconfigure_network } if (($anvil->data->{variables}{form}{config_step2}{$ip_key}{value}) && ($anvil->data->{variables}{form}{config_step2}{$ip_key}{value} ne "dhcp") && - (not $anvil->Validate->is_ipv4({ip => $anvil->data->{variables}{form}{config_step2}{$ip_key}{value}}))) + (not $anvil->Validate->ipv4({ip => $anvil->data->{variables}{form}{config_step2}{$ip_key}{value}}))) { # Something was set, but it isn't valid. $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, key => "log_0148", variables => { @@ -417,7 +417,7 @@ sub reconfigure_network }}); # Are we building bonded interfaces? - if ($anvil->Validate->is_mac({mac => $link2_mac})) + if ($anvil->Validate->mac({mac => $link2_mac})) { # Yup! my $say_network = ""; @@ -733,7 +733,7 @@ sub reconfigure_network rename_interface($anvil, $anvil->data->{network}{'local'}{mac_address}{$link2_mac}{interface}, $new_link2_iface); } } - elsif ((exists $anvil->data->{variables}{form}{config_step2}{$link1_key}{value}) && ($anvil->Validate->is_mac({mac => $anvil->data->{variables}{form}{config_step2}{$link1_key}{value}}))) + elsif ((exists $anvil->data->{variables}{form}{config_step2}{$link1_key}{value}) && ($anvil->Validate->mac({mac => $anvil->data->{variables}{form}{config_step2}{$link1_key}{value}}))) { ### NOTE: This only applies when configuring Striker dashboards. They can't ### be 'dhcp', either, so no checks are made for those cases. Likewise, @@ -1024,7 +1024,7 @@ sub get_uuid_from_interface_file { my $test_uuid = $1; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { test_uuid => $test_uuid }}); - if ($anvil->Validate->is_uuid({uuid => $test_uuid})) + if ($anvil->Validate->uuid({uuid => $test_uuid})) { $uuid = $test_uuid; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { uuid => $uuid }}); diff --git a/tools/anvil-download-file b/tools/anvil-download-file index faa17f4a..87caf4d0 100755 --- a/tools/anvil-download-file +++ b/tools/anvil-download-file @@ -112,7 +112,7 @@ sub get_job_details { my $job_uuid = $anvil->Job->get_job_uuid({debug => 2, program => $THIS_FILE}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { job_uuid => $job_uuid }}); - if ($anvil->Validate->is_uuid({uuid => $job_uuid})) + if ($anvil->Validate->uuid({uuid => $job_uuid})) { # Got one! $anvil->data->{switches}{'job-uuid'} = $job_uuid; diff --git a/tools/anvil-manage-keys b/tools/anvil-manage-keys index ff830b82..88939c97 100755 --- a/tools/anvil-manage-keys +++ b/tools/anvil-manage-keys @@ -369,7 +369,7 @@ sub load_job_data foreach my $state_uuid (split/,/, $job_data) { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { state_uuid => $state_uuid }}); - if ($anvil->Validate->is_uuid({uuid => $state_uuid})) + if ($anvil->Validate->uuid({uuid => $state_uuid})) { push @{$anvil->data->{state_uuids}}, $state_uuid; } diff --git a/tools/striker-get-peer-data b/tools/striker-get-peer-data index 997d09a2..042db3cb 100755 --- a/tools/striker-get-peer-data +++ b/tools/striker-get-peer-data @@ -263,7 +263,7 @@ fi; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { file_body => $file_body }}); my $host_uuid = ""; - if ((not $file_body) or (not $anvil->Validate->is_uuid({uuid => $file_body}))) + if ((not $file_body) or (not $anvil->Validate->uuid({uuid => $file_body}))) { # No good, Try dmidecode. my ($output, $error, $return_code) = $anvil->Remote->call({ @@ -279,7 +279,7 @@ fi; error => $error, return_code => $return_code, }}); - if ($anvil->Validate->is_uuid({uuid => $output})) + if ($anvil->Validate->uuid({uuid => $output})) { # Got it. $host_uuid = $output; @@ -334,7 +334,7 @@ sub get_password $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 0, priority => "err", key => "error_0083"}); $anvil->nice_exit({exit_code => 1}); } - elsif (not $anvil->Validate->is_uuid({uuid => $anvil->data->{switches}{'state-uuid'}})) + elsif (not $anvil->Validate->uuid({uuid => $anvil->data->{switches}{'state-uuid'}})) { print "connected=0\n"; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 0, priority => "err", key => "error_0082", variables => {state_uuid => $anvil->data->{switches}{'state-uuid'} }}); diff --git a/tools/striker-initialize-host b/tools/striker-initialize-host index 1c295e5c..36a18938 100755 --- a/tools/striker-initialize-host +++ b/tools/striker-initialize-host @@ -82,7 +82,7 @@ sub set_host_name $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, 'print' => 1, key => "job_0061", variables => { host_name => $anvil->data->{data}{host_name} }}); update_progress($anvil, $anvil->data->{job}{progress}, "job_0061,!!host_name!".$anvil->data->{data}{host_name}."!!"); - if (not $anvil->Validate->is_domain_name({name => $anvil->data->{data}{host_name}})) + if (not $anvil->Validate->domain_name({name => $anvil->data->{data}{host_name}})) { # Bad host_name. $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, 'print' => 1, key => "job_0062", variables => { host_name => $anvil->data->{data}{host_name} }}); @@ -163,7 +163,7 @@ sub add_databases interface => $interface, local_ip => $local_ip, }}); - if (($local_ip) && ($anvil->Validate->is_ipv4({ip => $local_ip}))) + if (($local_ip) && ($anvil->Validate->ipv4({ip => $local_ip}))) { $db_host = $local_ip; $db_port = $anvil->data->{database}{$host_uuid}{port}; diff --git a/tools/striker-manage-install-target b/tools/striker-manage-install-target index ab7d63f3..d7ccdaf1 100755 --- a/tools/striker-manage-install-target +++ b/tools/striker-manage-install-target @@ -462,7 +462,7 @@ sub setup_boot_environment next if $interface !~ /^bcn/; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { "network::local::${interface}::ip" => $anvil->data->{network}{'local'}{interface}{$interface}{ip} }}); - if ($anvil->Validate->is_ipv4({ip => $anvil->data->{network}{'local'}{interface}{$interface}{ip} })) + if ($anvil->Validate->ipv4({ip => $anvil->data->{network}{'local'}{interface}{$interface}{ip} })) { $bcn_interface = $interface; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { bcn_interface => $bcn_interface }}); diff --git a/tools/striker-manage-peers b/tools/striker-manage-peers index dfb02390..a79e86be 100755 --- a/tools/striker-manage-peers +++ b/tools/striker-manage-peers @@ -185,7 +185,7 @@ sub process_entry $anvil->data->{config}{rewrite} = 0; # Is anything missing? - if ((not $host_uuid) or (not $anvil->Validate->is_uuid({uuid => $host_uuid}))) + if ((not $host_uuid) or (not $anvil->Validate->uuid({uuid => $host_uuid}))) { # Invalid UUID. print $anvil->Words->string({key => "error_0031", variables => { host_uuid => $host_uuid }})."\n"; diff --git a/tools/striker-scan-network b/tools/striker-scan-network index f5e3c5c4..65eb2bed 100755 --- a/tools/striker-scan-network +++ b/tools/striker-scan-network @@ -193,8 +193,8 @@ sub scan my $subnet_mask = $2; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "switches::network" => $anvil->data->{switches}{network} }}); - my $ip_valid = $anvil->Validate->is_ipv4({ip => $ip}); - my $subnet_mask_valid = $anvil->Validate->is_ipv4({ip => $subnet_mask}); + my $ip_valid = $anvil->Validate->ipv4({ip => $ip}); + my $subnet_mask_valid = $anvil->Validate->ipv4({ip => $subnet_mask}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { ip_valid => $ip_valid, subnet_mask_valid => $subnet_mask_valid, @@ -330,7 +330,7 @@ sub call_nmap ip => $ip, subnet_mask => $subnet_mask, }}); - if ($anvil->Validate->is_ipv4({ip => $subnet_mask})) + if ($anvil->Validate->ipv4({ip => $subnet_mask})) { # Convert to CIDR my $cidr = $anvil->Convert->cidr({subnet_mask => $subnet_mask}); @@ -383,8 +383,8 @@ sub call_nmap }}); # Sane? - my $mac_valid = $anvil->Validate->is_hex({string => $this_mac, sloppy => 1}); - my $ip_valid = $anvil->Validate->is_ipv4({ip => $this_ip}); + my $mac_valid = $anvil->Validate->hex({string => $this_mac, sloppy => 1}); + my $ip_valid = $anvil->Validate->ipv4({ip => $this_ip}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { mac_valid => $mac_valid, ip_valid => $ip_valid,