initial commit of file from CVS for e-smith-hosts on Wed 12 Jul 08:56:08 BST 2023

This commit is contained in:
Brian Read
2023-07-12 08:56:08 +01:00
parent 58e6e39b6f
commit b45f3382c7
23 changed files with 3378 additions and 2 deletions

View File

@@ -0,0 +1,38 @@
{
foreach my $host ($DB->get_all)
{
next if defined $host->prop('HostType');
my %properties;
$properties{'InternalIP'} = $host->prop('InternalIP') || '';
$properties{'ExternalIP'} = $host->prop('ExternalIP') || '';
$properties{'MACAddress'} = $host->prop('MACAddress') || '';
# If defined as host 'self', change to new format
if (($properties{'InternalIP'} eq 'self') &&
($properties{'ExternalIP'} eq 'self'))
{
$properties{'HostType'} = 'Self';
$properties{'InternalIP'} = '';
$properties{'ExternalIP'} = '';
$properties{'MACAddress'} = '';
}
# defined as 'local' entry in new format
elsif( $properties{InternalIP} ne 'self' )
{
$properties{'HostType'} = 'Local';
$properties{ExternalIP} = '';
}
elsif( $properties{ExternalIP} ne 'self' )
{
$properties{'HostType'} = 'Remote';
$properties{InternalIP} = '';
}
else {
warn "$host has no InternalIP or ExternalIP!\n";
}
$host->delete_prop('Visibility');
$host->merge_props(%properties);
}
}

View File

@@ -0,0 +1,22 @@
{
foreach my $host ($DB->get_all)
{
my $lc_key = lc($host->key);
next if ( $lc_key eq $host->key );
my $rec = $DB->get($lc_key);
if ($rec)
{
warn "HostsDB: " . $host->key .
" NOT migrated as $lc_key exists\n";
next;
}
$rec = $DB->new_record($lc_key);
$rec->merge_props($host->props);
$host->delete;
}
}

View File

@@ -0,0 +1,20 @@
{
use esmith::DomainsDB;
my $ddb = esmith::DomainsDB->open_ro;
# If there's no domains db, there's nothing to do
return unless $ddb;
# For any bare hostname, create a qualified host record
# for each domain, then delete the bare host record
foreach my $host (grep { $_->key !~ /\./ } $DB->get_all)
{
my $key = $host->key;
foreach my $d ($ddb->domains)
{
$d = $d->key;
next if $DB->get("$key.$d");
$DB->new_record("$key.$d", { $host->props });
}
$host->delete;
}
}

View File

@@ -0,0 +1,10 @@
{
# Purge quoting chars in comments to fix bug 8723 & bug 8806
foreach my $host ($DB->get_all)
{
my $comment = $host->prop('Comment');
next unless $comment;
$comment =~ s/[^a-zA-Z0-9\ \_\-\,\.]+//g;
$host->merge_props(Comment => $comment);
}
}

View File

@@ -0,0 +1,28 @@
#!/bin/sh
#----------------------------------------------------------------------
# copyright (C) 1999-2005 Mitel Networks Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#----------------------------------------------------------------------
#------------------------------------------------------------
# Create the default hosts for the primary domain in
# bootstrap-console-save
#------------------------------------------------------------
exec /etc/e-smith/events/actions/create-default-hosts domain-create \
$(/sbin/e-smith/config get DomainName)

View File

@@ -0,0 +1,272 @@
#!/usr/bin/perl -w
#----------------------------------------------------------------------
# copyright (C) 2002-2005 Mitel Networks Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#----------------------------------------------------------------------
#--------------------------------------------------
# Names in the hosts database can be:
# - Internal name only (name for host on local LAN)
# hp4500=host|InternalIP|192.168.16.201|MacAddress|aa:bb:cc:dd:ee:ff
#
# - Hosted by e-smith server (internal/external on e-smith server)
# mail=host|ExternalIP|self|InternalIP|self
#
# - Externally hosted
# www=host|ExternalIP|a.b.c.d
#
# - Internal machine behind port-forwarding firewall
# secure=host|ExternalIP|d.e.f.g|InternalIP|w.x.y.z
#
# Unqualified names in the key apply to all domains
# FQDNs apply to specific domains and override the wildcard shortnames
#--------------------------------------------------
package esmith;
use strict;
use Errno;
use esmith::HostsDB;
my $hdb = esmith::HostsDB->open();
=begin testing
use esmith::TestUtils qw(scratch_copy);
my $h_tmp = '50-hosts/hosts.conf.4.1.2';
my $c_scratch = scratch_copy('50-hosts/configuration.conf');
$ENV{ESMITH_HOSTS_DB} = $h_tmp;
$ENV{ESMITH_CONFIG_DB} = $c_scratch;
# 4.1.2 Hosts DB which caused bug 2495
open(TMP, ">$h_tmp") || die $!;
print TMP <<'OUT';
archie=host|ExternalIP|209.28.139.23|InternalIP|self
e-smith=host|ExternalIP|self|InternalIP|self
e-smith-manager=host|ExternalIP|self|InternalIP|self
ftp=host|ExternalIP|self|InternalIP|self
mail=host|ExternalIP|self|InternalIP|self
proxy=host|ExternalIP|self|InternalIP|self
server=host|ExternalIP|self|InternalIP|self
www=host|ExternalIP|self|InternalIP|202.49.164.27
OUT
close TMP;
END { unlink $h_tmp }
system($^X, $Original_File);
use esmith::ConfigDB;
use esmith::HostsDB;
my $migrated = esmith::HostsDB->open;
my $config = esmith::ConfigDB->open;
my $domain = $config->get('DomainName')->value;
my $www = $migrated->get("www.$domain");
isa_ok( $www, 'esmith::DB::Record' );
is( $www->prop('InternalIP'), '202.49.164.27' );
is( $www->prop('ExternalIP'), '' );
is( $www->prop('HostType'), 'Local' );
my $archie = $migrated->get("archie.$domain");
isa_ok( $archie, 'esmith::DB::Record' );
is( $archie->prop('InternalIP'), '', 'InternalIP');
is( $archie->prop('ExternalIP'), '209.28.139.23', 'ExternalIP' );
is( $archie->prop('HostType'), 'Remote', 'HostType' );
my @migrated = grep { $_->key !~ /^(archie|www)/ }
$migrated->get_all_by_prop(type => 'host');
is_deeply( [ sort map { $_->key } @migrated ],
[ sort map { "$_.$domain" }
qw( e-smith e-smith-manager ftp mail proxy server )],
'domain names fully qualified'
);
foreach my $host (@migrated) {
my $dn = $host->key;
is( $host->prop('HostType'), 'Self', "$dn - HostType");
is( $host->prop('ExternalIP'), '' , " ExternalIP" );
is( $host->prop('InternalIP'), '' , " InternalIP" );
}
=end testing
=cut
#------------------------------------------------------------
# Populate the hosts database with default entries if they
# they don't already exist
#------------------------------------------------------------
my $event = $ARGV [0];
#------------------------------------------------------------
# Migrate existing hosts to new format for primary domain, and
# for any existing virtual domains
#
# eg. www=host|... -> www.{$DomainName}=host|...
#------------------------------------------------------------
my @FQDNHostList = ();
my @ShortHostList = ();
foreach my $host ($hdb->hosts)
{
if ($host->key =~ /\./)
{
# keep track of fully qualified domain name hosts
push @FQDNHostList, $host->key;
}
else
{
# keep track of single word hosts
push @ShortHostList, $host->key;
}
}
# Get the list of local domain names
use esmith::DomainsDB;
my $ddb = esmith::DomainsDB->open_ro();
my @domainsList = map { $_-> key } $ddb->domains;
eval "require esmith::NetworkServicesDB";
unless ($@)
{
my $nsdb = esmith::NetworkServicesDB->open();
if ($nsdb)
{
my $service_domain = $nsdb->service_domain();
push @domainsList, $service_domain if $service_domain;
}
}
#-------------------------------------------------------
# Iterate over each domain to propagate the single host
# entries for each virtual domain
#-------------------------------------------------------
foreach my $domain ( @domainsList )
{
# default visibility property is 'Local' as this never existed before
my %properties = (
Visibility => 'Local'
);
foreach my $host ( @ShortHostList )
{
$properties{'InternalIP'} =
$hdb->get_prop ($host, 'InternalIP') || '';
$properties{'ExternalIP'} =
$hdb->get_prop ($host, 'ExternalIP') || '';
$properties{'MACAddress'} =
$hdb->get_prop ($host, 'MACAddress') || '';
my $FQDN = "$host.$domain";
next if (defined $hdb->get ($FQDN));
# If defined as host 'self', change to new format
if (($properties{'InternalIP'} eq 'self') &&
($properties{'ExternalIP'} eq 'self'))
{
$properties{'HostType'} = 'Self';
$properties{'InternalIP'} = '';
$properties{'ExternalIP'} = '';
$properties{'MACAddress'} = '';
}
# defined as 'local' entry in new format
elsif( $properties{InternalIP} ne 'self' )
{
$properties{'HostType'} = 'Local';
$properties{ExternalIP} = '';
}
elsif( $properties{ExternalIP} ne 'self' )
{
$properties{'HostType'} = 'Remote';
$properties{InternalIP} = '';
}
else {
warn "$host has no InternalIP or ExternalIP!\n";
}
$properties{'type'} = 'host';
$hdb->new_record($FQDN, \%properties);
}
}
#-------------------------------------------------------
# Iterate over all of the fully qualified domain names,
# setting valid ones (from @domainsList) with appropriate
# values and flagging other non-expected ones with errors
#-------------------------------------------------------
foreach my $key ( @FQDNHostList )
{
# Skip the hosts if already in the new format
next if (defined $hdb->get_prop ($key, 'HostType'));
my %properties = (
Visibility => 'Local'
);
my ($tempHost, $tempDomain) = split /\./, $key, 2;
my @matches = grep(/$tempDomain/, @domainsList);
unless (@matches)
{
# doesn't match an existing domain, set error flag
$properties{'error'} = 'Host has no matching domain in system';
}
$properties{'InternalIP'} = $hdb->get_prop ($key, 'InternalIP') || '';
$properties{'ExternalIP'} = $hdb->get_prop ($key, 'ExternalIP') || '';
$properties{'MACAddress'} = $hdb->get_prop ($key, 'MACAddress') || '';
# If defined as host 'self', change to new format
if ($properties{'InternalIP'} eq 'self')
{
$properties{'HostType'} = 'Self';
$properties{'InternalIP'} = '';
$properties{'ExternalIP'} = '';
$properties{'MACAddress'} = '';
}
else # defined as 'local' entry in new format
{
$properties{'HostType'} = 'Local';
$properties{'ExternalIP'} = '';
}
$properties{'type'} = 'host';
$hdb->new_record($key, \%properties);
}
# Remove the old single host entries
foreach my $host ( @ShortHostList )
{
$hdb->get($host)->delete;
}
exit (0);

View File

@@ -0,0 +1,69 @@
#!/usr/bin/perl -w
#----------------------------------------------------------------------
# copyright (C) 1999-2005 Mitel Networks Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#----------------------------------------------------------------------
package esmith;
use strict;
use Errno;
use esmith::ConfigDB;
use esmith::HostsDB;
my $db = esmith::ConfigDB->open_ro();
my $hdb = esmith::HostsDB->open();
#------------------------------------------------------------
# Populate the hosts database with default entries for a given
# domain if they don't already exist
#------------------------------------------------------------
my $event = $ARGV [0] or die "Must provide event name arg";
my $domain = $ARGV [1] or die "Must provide domain name arg";
#------------------------------------------------------------
# Create a default host for the SystemName which includes
# a property static=yes (meaning not editable from the interface)
#
# Also create a set of default host records of type "Self".
#------------------------------------------------------------
my $systemName = $db->get('SystemName')->value;
foreach my $host (qw(ftp mail www proxy wpad), $systemName)
{
my $FQDN = join ".", $host, $domain;
next if (defined $hdb->get($FQDN));
my %properties = (
type => 'host',
HostType => 'Self',
InternalIP => '',
ExternalIP => '',
MACAddress => ''
);
if ($host eq $systemName)
{
$properties{static} = "yes";
}
$hdb->new_record($FQDN, \%properties);
}
exit (0);

View File

@@ -0,0 +1,67 @@
#!/usr/bin/perl -w
#----------------------------------------------------------------------
# copyright (C) 1999-2005 Mitel Networks Corporation
# Copyright (C) 2005 Gordon Rowell <gordonr@gormand.com.au>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#----------------------------------------------------------------------
package esmith;
use strict;
use esmith::util;
use esmith::DomainsDB;
use esmith::HostsDB;
use esmith::AccountsDB;
my $domainsdb = esmith::DomainsDB->open_ro;
my $hostsdb = esmith::HostsDB->open;
my $accountsdb = esmith::AccountsDB->open;
#------------------------------------------------------------
# Remove all hosts for the domain being deleted
#------------------------------------------------------------
my $event = $ARGV [0];
# my $domain = $ARGV[1]; # Unused
# Look for invalid hosts in the hostsdb.
my %domain_names = map { $_->{key} => 1 } $domainsdb->domains;
# We want to check each host and see if it belongs to a domain that we are
# currently managing. If not, delete it.
foreach my $host ($hostsdb->hosts)
{
my ($hostpart, $domainpart) = split (/\./, $host->{key}, 2);
next if exists $domain_names{$domainpart};
$host->delete;
}
# Ditto for pseudonyms
for my $nym ($accountsdb->pseudonyms)
{
my ($host, $domain) = split (/\@/, $nym->{key}, 2);
next unless $domain;
next if exists $domain_names{$domain};
$nym->delete;
}
exit 0;

View File

@@ -0,0 +1,288 @@
<lexicon lang="en-us" params="lexicon_params()">
<!-- vim: ft=xml
-->
<entry>
<base>Hostnames and addresses</base>
<trans>
Hostnames and addresses
</trans>
</entry>
<entry>
<base>UNABLE_TO_OPEN_CONFIGDB</base>
<trans>Unable to open configuration database</trans>
</entry>
<entry>
<base>DNS_FORWARDER_ENABLED</base>
<trans>
A DNS forwarder has been configured. This means that all DNS
lookups will be handled by the DNS forwarder. Hostnames
and addresses cannot be modified on this server while
a DNS forwarder is configured.
</trans>
</entry>
<entry>
<base>ADD_HOSTNAME</base>
<trans>
<![CDATA[
<a class="button-like" href="hostentries?page=0&page_stack=&Next=Next&wherenext=CreateModify">Add hostname</a>
]]>
</trans>
</entry>
<entry>
<base>HOSTNAME</base>
<trans>
Hostname
</trans>
</entry>
<entry>
<base>HOSTTYPE</base>
<trans>
Location
</trans>
</entry>
<entry>
<base>LOCAL_IP</base>
<trans>
Local IP
</trans>
</entry>
<entry>
<base>ETHERNET_ADDRESS</base>
<trans>
Ethernet address
</trans>
</entry>
<entry>
<base>CURRENT_HOSTNAMES_FOR_DOMAIN</base>
<trans>
Current list of hostnames for {$domain}.
</trans>
</entry>
<entry>
<base>NO_HOSTNAMES_FOR_SERVICENAME</base>
<trans>
There are no hostnames in the system for {$serviceName}.
</trans>
</entry>
<entry>
<base>CURRENT_HOSTNAMES_FOR_LOCAL_DOMAIN</base>
<trans>
Current list of hostnames for {$localDomainName}
</trans>
</entry>
<entry>
<base>NO_HOSTNAMES_FOR_LOCAL_DOMAIN</base>
<trans>
There are no hostnames in the system for {$localDomainName}.
</trans>
</entry>
<entry>
<base>STATIC_HOST_MESSAGE</base>
<trans>
- This host represents your system name and cannot be modified
or removed.
</trans>
</entry>
<entry>
<base>CREATE_LOCAL_HOST_TITLE</base>
<trans>
Create a new hostname for this server</trans>
</entry>
<entry>
<base>HOSTNAME_VALIDATION_ERROR</base>
<trans>
Error: unexpected characters in host name:
"{$HostName}". The host name should contain only
letters, numbers, and hyphens and must start
with a letter or a number.
</trans>
</entry>
<entry>
<base>HOSTNAME_LENGTH_ERROR</base>
<trans>
Error: account name {$HostName} is too long. The
maximum is 32 characters.
</trans>
</entry>
<entry>
<base>DOMAIN_VALIDATION_ERROR</base>
<trans>
Error: unexpected or missing characters in domain name
{$DomainName}. The domain name should contain one or more
letters, numbers, periods and minus signs. Did not create new
domain.
</trans>
</entry>
<entry>
<base>HOSTNAME_EXISTS_ERROR</base>
<trans>
Error: account {$fullHostName} is an existing {$type} hostname.
</trans>
</entry>
<entry>
<base>SUCCESSFULLY_CREATED</base>
<trans>
Successfully created hostname.
</trans>
</entry>
<entry>
<base>HOSTNAME_DESCRIPTION</base>
<trans>
The hostname must contain only letters, numbers, and hyphens,
and must start with a letter or number.
</trans>
</entry>
<entry>
<base>LOCAL_IP_DESCRIPTION</base>
<trans>
The Local IP address is the IP address of another machine on
the local network. Please enter a valid IP address in the
format "aaa.bbb.ccc.ddd".
</trans>
</entry>
<entry>
<base>ETHERNET_ADDRESS_DESCRIPTION</base>
<trans>
The ethernet address is optional and causes the DHCP server to
statically bind the local IP address to the computer with this
ethernet address. If specified, it must be of the form
"AA:BB:CC:DD:EE:FF" and must contain only the numbers 0-9 and
the letters A-F.
</trans>
</entry>
<entry>
<base>CREATE_LOCAL_HOST_TITLE</base>
<trans>
Create a new hostname referring to a local host.
</trans>
</entry>
<entry>
<base>DIDNT_ENTER_LOCAL_IP</base>
<trans>
Error: You did not specify a Local IP address. IP
addresses must contain only numbers and periods and
be in the form "aaa.bbb.ccc.ddd". Did not create hostname.
</trans>
</entry>
<entry>
<base>IP_VALIDATION_ERROR</base>
<trans>
Error: IP Address {$InternalIP} is
invalid. IP Addresses must contain only numbers and periods and be in the form "aaa.bbb.ccc.ddd". Did not create hostname.
</trans>
</entry>
<entry>
<base>MAC_ADDRESS_VALIDATION_ERROR</base>
<trans>
Error: Ethernet address {$MACAddress}
is invalid. Ethernet addresses must be in the
form "AA:BB:CC:DD:EE:FF" and only contain the
numbers 0-9 and the letters A-F. Did not create
hostname.
</trans>
</entry>
<entry>
<base>CREATE_REMOTE_HOST_TITLE</base>
<trans>
Create a new hostname referring to a remote host
</trans>
</entry>
<entry>
<base>CREATE_TITLE</base>
<trans>
Create or modify hostname
</trans>
</entry>
<entry>
<base>LOCAL_PAGE_DESCRIPTION</base>
<trans>
Please enter the following additional details for a local
host:
</trans>
</entry>
<entry>
<base>REMOTE_PAGE_DESCRIPTION</base>
<trans>
Please enter the following additional details for a remote
host:
</trans>
</entry>
<entry>
<base>SUCCESSFULLY_DELETED</base>
<trans>
Successfully deleted host.
</trans>
</entry>
<entry>
<base>SUCCESSFULLY_MODIFIED</base>
<trans>
Successfully modified host.
</trans>
</entry>
<entry>
<base>REMOVE_PAGE_DESCRIPTION</base>
<trans>
<![CDATA[
You are about to remove the hostname "{$hostname}.{$domain}".
</p>
<p>
<b>Are you sure you wish to remove this hostname?</b>
]]>
</trans>
</entry>
<entry>
<base>CONFIRM_DESCRIPTION</base>
<trans>
Please confirm the following details.
</trans>
</entry>
<entry>
<base>NO_HOSTS_FOR_THIS_DOMAIN</base>
<trans>
There are no hosts for this domain.
</trans>
</entry>
<entry>
<base>ADDR_IN_DHCP_RANGE</base>
<trans>Address is inside the DHCP assigned dynamic range</trans>
</entry>
<entry>
<base>ERROR_WHILE_CREATING_HOST</base>
<trans>Error occurred while creating hostname.</trans>
</entry>
<entry>
<base>ERROR_WHILE_MODIFYING_HOST</base>
<trans>Error occurred while modifying hostname.</trans>
</entry>
<entry>
<base>ERROR_WHILE_DELETING_HOST</base>
<trans>Error occurred while deleting hostname.</trans>
</entry>
<entry>
<base>ERR_IP_IS_LOCAL_OR_GATEWAY</base>
<trans>Error: IP cannot be server IP or Gateway IP.</trans>
</entry>
<entry>
<base>ERR_IP_NOT_LOCAL</base>
<trans>Error: This IP address is not on any of our local
networks.</trans>
</entry>
<entry>
<base>MUST_BE_VALID_HOSTNAME_OR_IP</base>
<trans>Must be a valid hostname or IP number</trans>
</entry>
<entry>
<base>HOSTNAME_COMMENT_ERROR</base>
<trans>
Error: unexpected characters in the comment of "{$hostname}.{$domain}".
The comment must contain only letters, spaces, numbers, dots, commas, undescores, hyphens and must start with a letter or number.
</trans>
</entry>
<entry>
<base>HOSTNAME_VALIDATOR_ERROR</base>
<trans>
Error: unexpected characters in host name: "{$hostname}.{$domain}". The host name should contain only
letters, numbers, and hyphens and must start with a letter or a number.
</trans>
</entry>
</lexicon>

View File

@@ -0,0 +1,27 @@
{
#------------------------------------------------------------
# Static dhcp Entries
#------------------------------------------------------------
use esmith::HostsDB;
my $hdb = esmith::HostsDB->open_ro();
foreach $host ($hdb->hosts)
{
my $InternalIP = $host->prop('InternalIP');
next unless (defined($InternalIP) && ($InternalIP ne ''));
my $MACAddress = $host->prop('MACAddress');
next unless ( defined($MACAddress) && ($MACAddress ne ''));
my $Filename = $host->prop('Filename');
$OUT .= "\n";
$OUT .= "host " . $host->key . " \{\n";
defined($Filename) and $OUT .= " filename \"$Filename\";\n";
$OUT .= " hardware ethernet $MACAddress;\n";
$OUT .= " fixed-address $InternalIP;\n";
$OUT .= "\}";
$OUT .= "\n";
}
undef; # We need a return value
}

View File

View File

@@ -0,0 +1,104 @@
# DO NOT MODIFY THIS FILE.
# This file is automatically maintained by the Mitel Networks SME Server
# configuration software. Manually editing this file may put your
# system in an unknown state.
#
# updated: Tue Apr 16 08:41:42 2002
AccessType=dedicated
ActiveAccounts=0
AdminEmail=
ConsoleMode=auto
ContactEmail=
ContactName=
ContactOrg=
DNSPrimaryIP=
DNSSecondaryIP=
DialupConnOffice=long
DialupConnOutside=long
DialupConnWeekend=long
DialupFreqOffice=every15min
DialupFreqOutside=everyhour
DialupFreqWeekend=everyhour
DialupModemDevice=/dev/ttyS1
DialupPhoneNumber=
DialupUserAccount=useraccount
DialupUserPassword=userpassword
DomainName=nssg.mitel.com
EmailUnknownUser=return
EthernetDriver1=pcnet32
EthernetDriver2=unknown
ExternalDHCP=off
ExternalNetmask=255.255.255.0
GatewayIP=10.33.15.1
LocalIP=10.33.15.127
LocalNetmask=255.255.255.0
MinUid=5000
NOCType=undefined
PasswordSet=yes
PreviousConfiguration=/home/e-smith/db/configuration.previous
SMTPSmartHost=
SambaDomainMaster=no
SambaServerName=mitel-networks-server
SambaWorkgroup=mitel-networks
ServiceAccountId=
ServiceDomainName=
ServiceTargetIP=
SquidParent=
SquidParentPort=
StatusReports=off
SystemMode=serveronly
SystemName=chocky
TimeZone=US/Eastern
UnsavedChanges=no
atalk=service|InitscriptOrder|91|status|enabled
auth=service|access|public|status|enabled
blades=service|Host|service.e-smith.com|status|enabled
bootstrap-console=service|InitscriptOrder|35|Run|no|status|enabled
branding=service|modified|000000000000|status|enabled
broker=service|DbHost|localhost|DbPass|nocpassword|DbPort|3306|DbUser|nocuser
crond=service|InitscriptOrder|40|status|enabled
ctrlaltdel=service|status|enabled
dhcpd=service|InitscriptOrder|65|end|10.33.15.250|start|10.33.15.65|status|disabled
diald=service|InitscriptOrder|57|status|disabled
fetchmail=service|FreqOffice|every5min|FreqOutside|every30min|FreqWeekend|never|Method|standard|SecondaryMailAccount|popaccount|SecondaryMailPassword|poppassword|SecondaryMailServer|mail.myisp.xxx|status|disabled
flexbackup=backupservice|erase_rewind_only|true
ftp=service|access|private|accessLimits|off|status|enabled
hdparm=service|InitscriptOrder|40|status|disabled
horde=service|status|disabled
httpd-admin=service|InitscriptOrder|86|status|enabled
httpd-e-smith=service|InitscriptOrder|85|access|private|status|enabled
imap=service|access|private|status|enabled
imp=service|status|disabled
ippp=service|InitscriptOrder|55|status|enabled
ipsec=service|InitscriptOrder|90|PubKey|0sAQNRVkVir0fuYdTrXFJlWdQuBB3Z67JqdqjPJzIdxCec/ci4PgJ6nA6zsTe+VqVYzFwCz2MudYlEN0pzwMaNcXdFXhPXhb969klS90UhgB2OdImwqGtt++si/WJ9DT3NdGRFYIw1hPibd5NzjFi3fNVS4lW8oU4I4ic0q7vYVpXu2Q==|status|disabled
isdn=service|Protocol|2|UseSyncPPP|yes|status|disabled
keytable=service|InitscriptOrder|25|status|enabled
ldap=service|InitscriptOrder|80|access|private|defaultCity|Ottawa|defaultCompany|XYZ Corporation|defaultDepartment|Main|defaultPhoneNumber|555-5555|defaultStreet|123 Main Street|status|enabled
lilo=service|AddressMode|linear
local=service|InitscriptOrder|99|status|enabled
lpd=service|InitscriptOrder|60|status|enabled
mariadb=service|InitscriptOrder|90|status|enabled
masq=service|InitscriptOrder|06|Logging|none|Stealth|no|status|disabled
modSSL=service|status|enabled
mysql.init=service|InitscriptOrder|99|status|enabled
named=service|chroot|yes|status|enabled
network=service|InitscriptOrder|10|status|enabled
ntpd=service|InitscriptOrder|55|status|disabled
php=service|status|enabled
popd=service|access|private|status|enabled
pppoe=service|DemandIdleTime|no|InitscriptOrder|57|SynchronousPPP|no|status|disabled
pptpd=service|sessions|10|status|disabled
qmail=service|InitscriptOrder|80|status|enabled
random=service|InitscriptOrder|20|status|enabled
rsyslog=service|InitscriptOrder|05|status|enabled
scanner=service|ScannerFns|iscan|UpdateTime|24:17|scanMail|yes|status|enabled
smb=service|DomainMaster|no|InitscriptOrder|91|RoamingProfiles|no|ServerName|chocky|Workgroup|mitel-networks|status|enabled
smtpfwdd=service|InitscriptOrder|81|status|enabled
squid=service|InitscriptOrder|90|status|enabled
sshd=service|InitscriptOrder|85|PasswordAuthentication|yes|PermitRootLogin|yes|access|private|status|enabled
sync=service|Host|service.e-smith.com|LastId|0|SuccessId|0|SyncFrequency|1|SyncMinute|36|status|disabled
telnet=service|access|private|status|disabled
testing=service|destruction|0
tinydns=service|InitscriptOrder|81|NOCServer|service.e-smith.com|Nameservers|ns1.e-smith.com,ns2.e-smith.com,ns3.e-smith.com,ns4.e-smith.com|PublishDomain|e-smith.net|SOA_Expiry|1048576|SOA_Minimum|2560|SOA_Refresh|16384|SOA_Retry|2048|status|enabled
twig=service|status|disabled
xinetd=service|InitscriptOrder|50|status|enabled

View File

@@ -0,0 +1,10 @@
# DO NOT MODIFY THIS FILE.
# This file is automatically maintained by the Mitel Networks SME Server
# configuration software. Manually editing this file may put your
# system in an unknown state.
#
# updated: Wed May 8 09:45:28 2002
bar.invalid.tld=host|ExternalIP||HostType|Self|InternalIP||MACAddress||Visibility|Local
baz.invalid.tld=host|ExternalIP||HostType|Self|InternalIP||MACAddress||Visibility|Local
foo.invalid.tld=host|ExternalIP||HostType|Self|InternalIP||MACAddress||Visibility|Local
wibble.invalid.tld=host|ExternalIP|InternalIP|MACAddress||Visibility|self

View File

@@ -0,0 +1,137 @@
#!/usr/bin/perl -wT
# vim: set ft=xml:
#----------------------------------------------------------------------
# heading : Configuration
# description : Hostnames and addresses
# navigation : 6000 6500
#----------------------------------------------------------------------
#----------------------------------------------------------------------
# copyright (C) 2002 Mitel Networks Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Technical support for this program is available from Mitel Networks
# Please visit our web site www.mitel.com/sme/ for details.
#----------------------------------------------------------------------
use strict;
use esmith::FormMagick::Panel::hostentries;
my $fm = esmith::FormMagick::Panel::hostentries->new();
$fm->display();
=begin testing
use esmith::FormMagick::Tester;
use esmith::TestUtils;
use esmith::TestUtils;
use esmith::ConfigDB;
use esmith::AccountsDB;
my $panel = $Original_File;
my $ua = esmith::FormMagick::Tester->new();
my $c = esmith::ConfigDB->open();
my $a = esmith::AccountsDB->open();
is (mode($panel), '4750', "Check permissions on script");
ok ($ua->get_panel($panel), "ABOUT TO RUN L10N TESTS");
is ($ua->{status}, 200, "200 OK");
#like($ua->{content}, qr/FORM_TITLE/, "Saw untranslated form title");
ok ($ua->set_language("en-us"), "Set language to U.S. English");
ok ($ua->get_panel($panel), "Get panel");
is ($ua->{status}, 200, "200 OK");
like($ua->{content}, qr/Hostnames and addresses/, "Saw translated form title");
ok ($ua->get_panel($panel), "ABOUT TO TEST CREATING HOST");
ok ($ua->follow("Click here"), "Follow 'create host' link");
is ($ua->{status}, 200, "200 OK");
like($ua->{content}, qr/Create\/modify host/, "Saw page
title");
like($ua->{content}, qr/Publish globally/, "Saw fields");
like($ua->{content}, qr/Next/, "Saw next button");
ok ($ua->get_panel($panel), "ABOUT TO TEST MODIFYING HOST");
ok ($ua->follow("Modify"), "Follow 'modify' link");
is ($ua->{status}, 200, "200 OK");
ok ($ua->get_panel($panel), "ABOUT TO TEST REMOVING HOST");
ok ($ua->follow("Remove"), "Follow 'remove' link");
is ($ua->{status}, 200, "200 OK");
=end testing
=cut
__DATA__
<form title="Hostnames and addresses" header="/etc/e-smith/web/common/head.tmpl"
footer="/etc/e-smith/web/common/foot.tmpl">
<page name="First" pre-event="print_status_message()">
<subroutine src="print_hosts_tables()" />
</page>
<page name="CreateModify" pre-event="turn_off_buttons()" post-event="more_options()">
<title>CREATE_TITLE</title>
<subroutine src="print_hostname_field()" />
<subroutine src="print_domain_field()" />
<field
type="text" size="30"
id="comment" >
<label>COMMENT</label>
</field>
<field type="select"
id="hosttype"
options="'Self' => 'SELF', 'Local' => 'LOCAL', 'Remote' => 'REMOTE'"
value='Self'>
<label>LOCATION</label>
</field>
<subroutine src="print_button('NEXT')" />
</page>
<page name="Remove" pre-event="turn_off_buttons()" post-event="remove()">
<description>REMOVE_PAGE_DESCRIPTION</description>
<subroutine src="print_button('REMOVE')" />
</page>
<page name="Local" pre-event="turn_off_buttons()"
post-event="goto_confirm()">
<description>LOCAL_PAGE_DESCRIPTION</description>
<field type="text" id="local_ip"
validation="ip_number, not_in_dhcp_range, not_taken, must_be_local">
<label>LOCAL_IP</label>
<description>LOCAL_IP_DESCRIPTION</description>
</field>
<field type="text" id="ethernet_address"
validation="mac_address_or_blank">
<label>ETHERNET_ADDRESS</label>
<description>ETHERNET_ADDRESS_DESCRIPTION</description>
</field>
<subroutine src="print_button('NEXT')" />
</page>
<page name="Remote" pre-event="turn_off_buttons()" post-event="goto_confirm()">
<description>REMOTE_PAGE_DESCRIPTION</description>
<field type="text" id="global_ip" validation="hostname_or_ip">
<label>IP_ADDRESS_OR_FQDN</label>
</field>
<subroutine src="print_button('NEXT')" />
</page>
<page name="Confirm" pre-event="turn_off_buttons()"
post-event="create_or_modify()">
<description>CONFIRM_DESCRIPTION</description>
<subroutine src="print_confirmation_details('FINISH')" />
<subroutine src="print_save_or_add_button()" />
</page>
</form>

View File

View File

@@ -0,0 +1,736 @@
#!/usr/bin/perl -w
#----------------------------------------------------------------------
# copyright (C) 1999-2005 Mitel Networks Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#----------------------------------------------------------------------
package esmith::FormMagick::Panel::hostentries;
use strict;
use esmith::FormMagick;
use esmith::ConfigDB;
use esmith::DomainsDB;
use esmith::HostsDB;
use esmith::NetworksDB;
use esmith::cgi;
use esmith::util;
use File::Basename;
use Exporter;
use Carp;
use Net::IPv4Addr;
use HTML::Entities;
our @ISA = qw(esmith::FormMagick Exporter);
our @EXPORT = qw(
print_hosts_tables
print_hostname_field
print_domain_field
domains_list
more_options
goto_confirm
print_confirmation_details
create_or_modify
lexicon_params
remove
mac_address_or_blank
not_in_dhcp_range
print_save_or_add_button
not_taken
must_be_local
hostname_or_ip
);
our $VERSION = sprintf '%d.%03d', q$Revision: 1.54 $ =~ /: (\d+).(\d+)/;
our $db = esmith::ConfigDB->open();
=pod
=head1 NAME
esmith::FormMagick::Panels::hostentries - useful panel functions
=head1 SYNOPSIS
use esmith::FormMagick::Panels::hostentries;
my $panel = esmith::FormMagick::Panel::hostentries->new();
$panel->display();
=head1 DESCRIPTION
=head2 new();
Exactly as for esmith::FormMagick
=begin testing
$ENV{ESMITH_ACCOUNT_DB} = "10e-smith-base/accounts.conf";
$ENV{ESMITH_CONFIG_DB} = "10e-smith-base/configuration.conf";
use_ok('esmith::FormMagick::Panel::hostentries');
use vars qw($panel);
ok($panel = esmith::FormMagick::Panel::hostentries->new(),
"Create panel object");
isa_ok($panel, 'esmith::FormMagick::Panel::hostentries');
=end testing
=cut
sub new
{
shift;
my $self = esmith::FormMagick->new();
$self->{calling_package} = (caller)[0];
bless $self;
# Uncomment the next line for debugging.
#$self->debug(1);
return $self;
}
=head2 hostname_or_ip
Validation routine for the remote address field for network printers.
=for testing
my $fm = esmith::FormMagick::Panel::hostentries->new();
can_ok('main', 'hostname_or_ip');
can_ok('CGI::FormMagick::Validator', 'ip_number');
isnt(CGI::FormMagick::Validator::ip_number($fm, '1.2.3'), "OK", "ip_number works OK");
like(CGI::FormMagick::Validator::ip_number($fm, '1.2.3'), qr(enough octets), "ip_number works OK");
foreach ("1.2.3.4", "255.255.255.255", "foo.com", "foo.bar.com", "123.foo.xxx"){
is(hostname_or_ip($fm, $_), "OK", "$_ should be OK");
}
foreach ("1.2.3", "foo bar", "foo;bar;com", " ", undef) {
isnt(hostname_or_ip($fm, $_), "OK", "$_ should not be OK");
}
=cut
sub hostname_or_ip
{
my ($fm, $data) = @_;
if ($data =~ /^[\d\.]+$/) {
if (CGI::FormMagick::Validator::ip_number($fm, $data) eq "OK")
{
return "OK";
}
else
{
return "MUST_BE_VALID_HOSTNAME_OR_IP";
}
}
elsif ($data =~ /^([a-zA-Z0-9\.\-]+)$/ )
{
return "OK";
}
else
{
return "MUST_BE_VALID_HOSTNAME_OR_IP";
}
}
=head1 HTML GENERATION ROUTINES
Routines for generating chunks of HTML needed by the panel.
=for testing
use_ok('esmith::DomainsDB');
my $d = esmith::DomainsDB->create();
isa_ok($d, 'esmith::DomainsDB');
can_ok($d, 'domains');
#can_ok($d, 'get_by_prop');
=cut
sub print_hosts_tables
{
my $self = shift;
my $q = $self->{cgi};
unless ($db)
{
$self->error('UNABLE_TO_OPEN_CONFIGDB', 'First');
$self->print_status_message();
return undef;
}
unless (scalar @{$self->domains_list()})
{
$self->success('DNS_FORWARDER_ENABLED', 'First');
$self->print_status_message();
return undef;
}
print $q->start_Tr,"<td>",$q->start_table ({-CLASS => "sme-noborders"}),"\n";
print $q->start_Tr,"<td>",$self->localise('ADD_HOSTNAME'),"</td>",$q->end_Tr,"\n";
my $hosts_db = esmith::HostsDB->open();
foreach my $d (@{$self->domains_list()})
{
print $q->start_Tr,"<td>","\n";
print $q->h3($self->localise('CURRENT_HOSTNAMES_FOR_DOMAIN', {domain => $d})),"\n";
print "</td>",$q->end_Tr,"\n";
if (my @hosts = $hosts_db->get_hosts_by_domain($d))
{
print $q->start_Tr,"<td>\n",$q->start_table ({-CLASS => "sme-border"}),"\n";
print $q->Tr (esmith::cgi::genSmallCell($q, $self->localise('HOSTNAME'),"header"),
esmith::cgi::genSmallCell($q, $self->localise('LOCATION'),"header"),
esmith::cgi::genSmallCell($q, $self->localise('IP_ADDRESS_OR_FQDN'),"header"),
esmith::cgi::genSmallCell($q, $self->localise('ETHERNET_ADDRESS'),"header"),
esmith::cgi::genSmallCell($q, $self->localise('COMMENT'),"header"),
esmith::cgi::genSmallCell($q, $self->localise('ACTION'),"header",2));
$self->print_host_row($_) foreach @hosts;
print $q->end_table, "\n";
print "</td>",$q->end_Tr,"\n";
}
else
{
print $q->start_Tr,"<td>\n";
print $self->localise('NO_HOSTS_FOR_THIS_DOMAIN');
print "</td>",$q->end_Tr,"\n";
}
}
print $q->end_table,"\n";
return undef;
}
=head2 $panel->print_table_headers()
Prints the table header row for the hosts tables.
=for testing
can_ok($panel, 'print_table_headers');
can_ok($panel, 'localise');
$panel->print_table_headers;
like($_STDOUT_, qr(HOSTNAME), "Printed table headers");
=cut
sub print_table_headers
{
}
sub print_host_row
{
my ($self,$host_record) = @_;
my $q = $self->{cgi};
my $ht = $host_record->prop('HostType');
my $ip =
($ht eq 'Self') ? $db->get_value('LocalIP') :
($ht eq 'Remote') ? $host_record->prop('ExternalIP') :
$host_record->prop('InternalIP');
print $q->start_Tr;
$self->print_td($host_record->key());
$self->print_td($self->localise($host_record->prop('HostType')) || "&nbsp;");
$self->print_td($ip);
$self->print_td($host_record->prop('MACAddress') || "&nbsp;");
$self->print_td(HTML::Entities::encode($host_record->prop('Comment')) || "&nbsp;");
my $static = $host_record->prop('static') || "no";
if ($static ne 'yes') {
my $propstring = $self->build_host_cgi_params($host_record->key(), $host_record->props());
$self->print_td(qq(<a href="hostentries?wherenext=CreateModify&$propstring">)
. $self->localise('MODIFY')
. qq(</a>\n));
$self->print_td(qq(<a href="hostentries?wherenext=Remove&$propstring">)
. $self->localise('REMOVE')
. qq(</a>\n));
} else {
$self->print_td("&nbsp;");
$self->print_td("&nbsp;");
}
print "</tr>\n";
}
=for testing
$panel->print_td("foo");
like($_STDOUT_, qr(<td>foo</td>), "print_td");
=cut
sub print_td
{
my ($self, $value) = @_;
print "<td class=\"sme-border\">$value</td>\n";
}
sub build_host_cgi_params {
my ($self, $hostname, %oldprops) = @_;
my ($host, $domain) = $self->split_hostname($hostname);
my %props = (
page => 0,
page_stack => "",
".id" => $self->{cgi}->param('.id') || "",
name => $host,
domain => $domain,
local_ip => $oldprops{InternalIP},
global_ip => $oldprops{ExternalIP},
ethernet_address => $oldprops{MACAddress},
hosttype => $oldprops{HostType},
comment => HTML::Entities::encode($oldprops{Comment}),
);
return $self->props_to_query_string(\%props);
}
=for testing
my @expect = qw(foo bar.com);
is_deeply(\@expect, [$panel->split_hostname("foo.bar.com")], "Split hostname");
=cut
sub split_hostname {
my ($self, $hostname) = @_;
return ($hostname =~ /^([^\.]+)\.(.+)$/);
}
sub print_hostname_field {
my $fm = shift;
print qq(<tr><td colspan=2>) . $fm->localise('HOSTNAME_DESCRIPTION') . qq(</td></tr>);
print qq(<tr><td class="sme-noborders-label">) . $fm->localise('HOSTNAME') . qq(</td>\n);
my $h = $fm->{cgi}->param('name');
if ($h) {
print qq(
<td>$h
<input type="hidden" name="name" value="$h">
<input type="hidden" name="action" value="modify">
</td>
);
} else {
print qq(
<td><input type="text" name="name">
<input type="hidden" name="action" value="create">
</td>
);
}
print qq(</tr>\n);
return undef;
}
sub print_domain_field
{
my $fm = shift;
print qq( <tr>) ."\n". qq( <td class="sme-noborders-label">) . $fm->localise('DOMAIN') . qq(</td>\n);
my $h = $fm->{cgi}->param('name');
my $dom = $fm->{cgi}->param('domain');
if ($h) {
print qq(
<td>$dom
<input type="hidden" name="domain" value="$dom">
</td>
);
} else {
print qq(
<td>
<select type="select" name="domain">\n);
foreach my $d (@{$fm->domains_list()})
{
print " <option>$d\n";
}
print qq(
</select>
</td>\n);
}
print qq( </tr>\n);
return undef;
}
=head2 mac_address_or_blank
Validation routine for optional ethernet address
=for testing
can_ok('main', 'mac_address_or_blank');
is(mac_address_or_blank(undef, ""), "OK", "blank mac address is OK");
is(mac_address_or_blank(undef, "aa:bb:cc:dd:ee:ff"), "OK", "OK mac address is OK");
isnt(mac_address_or_blank(undef, "blah"), "OK", "wrong mac address is not OK");
=cut
sub mac_address_or_blank {
my ($fm, $data) = @_;
return "OK" unless $data;
return CGI::FormMagick::Validator::mac_address($fm, $data);
}
=for testing
can_ok('main', 'domains_list');
=cut
sub domains_list
{
my $self = shift;
my $d = esmith::DomainsDB->open_ro() or die "Couldn't open DomainsDB";
my @domains;
for ($d->domains)
{
my $ns = $_->prop("Nameservers") || 'localhost';
push @domains, $_->key if ($ns eq 'localhost');
}
return \@domains;
}
=for testing
can_ok('main', 'more_options');
=cut
sub more_options
{
my $self = shift;
my $q = $self->{cgi};
my $hostsdb = esmith::HostsDB->open_ro;
my $hostname = lc $q->param('name');
my $domain = lc $q->param('domain');
my $fqdn = "$hostname.$domain";
my $comment = $q->param('comment');
$self->cgi->param(-name=>'name', -value=>$hostname);
unless ( $hostname =~ /^[a-z0-9][a-z0-9-]*$/ )
{
return $self->error('HOSTNAME_VALIDATOR_ERROR');
}
unless ( $comment =~ /^([a-zA-Z0-9][\_\.\-,A-Za-z0-9\s]*)$/ || $comment eq '' )
{
return $self->error('HOSTNAME_COMMENT_ERROR');
}
# Look for duplicate hosts.
my $hostrec = undef;
if ($self->cgi->param('action') eq 'create' and $hostrec = $hostsdb->get($fqdn))
{
return $self->error(
$self->localise('HOSTNAME_EXISTS_ERROR',
{fullHostName => $fqdn,
type => $hostrec->prop('HostType')}));
}
my $hosttype = $self->cgi->param('hosttype');
if ($hosttype eq 'Self') {
$self->wherenext('Confirm');
} elsif ($hosttype eq 'Local') {
$self->wherenext('Local');
} elsif ($hosttype eq 'Remote') {
$self->wherenext('Remote');
} else {
$self->wherenext('Confirm');
}
}
=for testing
can_ok('main', 'goto_confirm');
=cut
sub goto_confirm
{
my $self = shift;
$self->wherenext('Confirm');
}
=for testing
can_ok('main', 'print_confirmation_details');
=cut
sub print_confirmation_details {
my ($self) = @_;
my $q = $self->{cgi};
print $q->start_table ({-CLASS => "sme-border"}),"\n";
my $type = $self->{cgi}->param('hosttype') || '';
if ($type eq "Self")
{
$self->{cgi}->delete('local_ip');
$self->{cgi}->delete('global_ip');
$self->{cgi}->delete('ethernet_address');
}
if ($type eq "Remote")
{
$self->{cgi}->delete('local_ip');
$self->{cgi}->delete('ethernet_address');
}
if ($type eq "Local")
{
$self->{cgi}->delete('global_ip');
}
my %label_map = (
global_ip => "IP_ADDRESS_OR_FQDN",
local_ip => "IP_ADDRESS",
);
foreach my $f (qw( name domain hosttype local_ip global_ip
ethernet_address comment) ) {
my $val = $self->cgi->param($f) || '';
$self->debug_msg("looping on param $f, val is $val");
next unless $val;
my $label = $label_map{$f} || uc($f);
print $q->Tr (esmith::cgi::genSmallCell($q, $self->localise($label),"normal"),
esmith::cgi::genSmallCell($q, HTML::Entities::encode($val),"normal"));
}
print qq(</table>);
return "";
}
=head2 create_or_modify()
This is the subroutine that does the actual work when you create/modify
a host.
=begin testing
#$ENV{ESMITH_HOSTS_DB} = scratch_copy("50-hosts/hosts.conf");
$ENV{ESMITH_HOSTS_DB} = "50-hosts/hosts.conf";
ok($panel = esmith::FormMagick::Panel::hostentries->new(),
"Create panel object");
isa_ok($panel, 'esmith::FormMagick::Panel::hostentries');
$panel->{cgi} = CGI->new({
name => 'wibble',
domain => 'invalid.tld',
hosttype => 'Self',
action => 'create',
});
$panel->create_or_modify();
isnt($panel->cgi->param('status_message'), undef, "Set status message");
my $h = esmith::HostsDB->open();
ok($h->get('wibble.invalid.tld'), "Host added to db");
=end testing
=cut
sub create_or_modify {
my ($self) = @_;
$self->wherenext('First');
my $h = esmith::HostsDB->open() || esmith::HostsDB->create();
my $hostname = $self->cgi->param('name') . "." .
$self->cgi->param('domain');
# Untaint and lowercase $hostname
$hostname =~ /([\w\.-]+)/; $hostname = lc($1);
my %props = (
type => 'host',
HostType => $self->cgi->param('hosttype') || "",
ExternalIP => $self->cgi->param('global_ip') || "",
InternalIP => $self->cgi->param('local_ip') || "",
MACAddress => $self->cgi->param('ethernet_address') || "",
Comment => $self->cgi->param('comment') || "",
);
if ($self->cgi->param('action') eq 'create') {
if ($h->new_record($hostname, \%props)) {
if (system("/sbin/e-smith/signal-event", "host-create", $hostname) == 0) {
return $self->success('SUCCESSFULLY_CREATED');
}
}
return $self->error('ERROR_WHILE_CREATING_HOST');
} else {
my $record = $h->get($hostname);
if ($record->merge_props(%props)) {
if (system("/sbin/e-smith/signal-event", "host-modify", $hostname) == 0) {
return $self->success('SUCCESSFULLY_MODIFIED');
}
}
$self->error('ERROR_WHILE_MODIFYING_HOST');
}
}
=head2 print_save_or_add_button()
=cut
sub print_save_or_add_button {
my ($self) = @_;
if ($self->cgi->param('action') eq 'modify') {
$self->print_button("SAVE");
} else {
$self->print_button("ADD");
}
}
=head2 remove()
=for testing
can_ok('main', 'remove');
=cut
sub remove {
my ($self) = @_;
$self->wherenext('First');
my $h = esmith::HostsDB->open();
my $hostname = $self->cgi->param('name') . "." .
$self->cgi->param('domain');
# Untaint $hostname before use in system()
$hostname =~ /([\w\.-]+)/; $hostname = $1;
my $record = $h->get($hostname);
if ($record->delete()) {
if (system("/sbin/e-smith/signal-event", "host-delete", $hostname) == 0) {
return $self->success('SUCCESSFULLY_DELETED');
}
}
return $self->error('ERROR_WHILE_DELETING_HOST');
}
=head2 lexicon_params()
Provides lexicon parameters for interpolation.
=for testing
can_ok('main', 'lexicon_params');
my $panel = esmith::FormMagick::Panel::hostentries->new();
my %expect = (
hostname => 'wibble',
domain => 'invalid.tld'
);
$panel->{cgi} = CGI->new({
name => 'wibble',
domain => 'invalid.tld'
});
is_deeply({lexicon_params($panel)}, \%expect, "Get lexicon params");
=cut
sub lexicon_params {
my ($self) = @_;
return (
hostname => $self->cgi->param('name') || "",
domain => $self->cgi->param('domain') || "",
);
}
sub not_in_dhcp_range
{
my $self = shift;
my $address = shift;
my $status = $db->get('dhcpd')->prop('status') || "disabled";
return "OK" unless $status eq "enabled";
my $start = $db->get('dhcpd')->prop('start');
my $end = $db->get('dhcpd')->prop('end');
return (esmith::util::IPquadToAddr($start)
<= esmith::util::IPquadToAddr($address)
&&
esmith::util::IPquadToAddr($address)
<= esmith::util::IPquadToAddr($end)) ?
"ADDR_IN_DHCP_RANGE" :
"OK";
}
=head2 not_taken
This function checks the local ip address being set in the panel, and
ensures that it is not already taken as the gateway ip or the local ip
of the server.
=cut
sub not_taken
{
my $self = shift;
my $q = $self->{cgi};
my $localip = $q->param('local_ip');
my $server_localip = $db->get_value('LocalIP') || '';
my $server_gateway = $db->get_value('GatewayIP') || '';
my $server_extip = $db->get_value('ExternalIP') || '';
$self->debug_msg("\$localip is $localip");
$self->debug_msg("\$server_localip is $server_localip");
$self->debug_msg("\$server_gateway is $server_gateway");
$self->debug_msg("\$server_extip is $server_extip");
if ($localip eq $server_localip)
{
return 'ERR_IP_IS_LOCAL_OR_GATEWAY';
}
elsif ($localip eq $server_gateway)
{
return 'ERR_IP_IS_LOCAL_OR_GATEWAY';
}
elsif (($db->get_value('SystemMode') ne 'serveronly') &&
($server_extip eq $localip))
{
return 'ERR_IP_IS_LOCAL_OR_GATEWAY';
}
elsif ($localip eq '127.0.0.1')
{
return 'ERR_IP_IS_LOCAL_OR_GATEWAY';
}
else
{
return 'OK';
}
}
sub must_be_local
{
my $self = shift;
my $q = $self->{cgi};
my $localip = $q->param('local_ip');
# Make sure that the IP is indeed local.
my $ndb = esmith::NetworksDB->open_ro;
my @local_list = $ndb->local_access_spec;
foreach my $spec (@local_list)
{
next if $spec eq '127.0.0.1';
if (eval{Net::IPv4Addr::ipv4_in_network($spec, $localip)})
{
return 'OK';
}
}
# Not OK. The IP is not on any of our local networks.
return 'ERR_IP_NOT_LOCAL';
}
1;