uucp/utils/bootstrap.pl

176 lines
3.3 KiB
Perl
Executable File

#!/usr/bin/perl
#
use warnings;
use strict;
use Digest::MD5 qw(md5_hex);
use Getopt::Std;
my $myNode = "";
my $passwdFile = "passwd";
my $callFile = "call";
my $confFile = "config";
my $sysFile = "sys";
my $portFile = "port";
my %options = ();
sub getNodes {
my $nodelist = "../nodelist";
open(my $data, '<', $nodelist) or die ("Cannot open nodelist");
if ($options{t}) {
print("Truncating existing files\n");
truncate($passwdFile,0);
truncate($callFile,0);
truncate($confFile,0);
truncate($sysFile,0);
truncate($portFile,0);
}
print("Generating files\n");
while (my $line = <$data>) {
chomp $line;
my @fields = split(",", $line);
my $nodeName = $fields[0];
#$nodeName eq $myNode && continue
my $nodeAddress = $fields[1];
generatePort($nodeName,$nodeAddress);
generateSys($nodeName);
generateCall($nodeName);
generatePasswd($nodeName);
getKeys($nodeAddress);
print("System ".$nodeName." added successfully\n");
}
generateConfig($myNode);
print("Done\n\n");
}
sub generatePort {
my $nodeName = shift;
my $nodeAddress = shift;
my $currentNode = ucfirst($nodeName);
my $template = qq{
port $currentNode
type pipe
command /usr/bin/ssh -a -x -q -i /var/spool/uucp/.ssh/id_rsa -l uucp $nodeAddress
reliable true
protocol etyig
};
open(FH, '>>', $portFile);
print FH $template;
}
sub generateSys {
my $nodeName = shift;
my $currentNode = ucfirst($nodeName);
my $template = qq{
system $nodeName
call-login *
call-password *
called-login U$nodeName
local-send /
local-receive /var/spool/uucppublic
remote-send /
remote-receive /var/spool/uucppublic
time any
forward ANY
chat \"\" \\d\\d\\r\\c login: \\d\\L word: \\P
chat-timeout 60
protocol t
port $currentNode
};
open(FH, '>>', $sysFile);
print FH $template;
}
sub generateConfig {
my $nodeName = shift;
open(FH, '>>',$confFile);
my $nodeConfig = "nodename\t\t".$nodeName."\n";
print FH $nodeConfig;
}
sub getKeys {
my $address = shift;
my $cmd = "/usr/bin/ssh-keyscan -H $address >> /var/spool/uucp/.ssh/known_hosts";
system($cmd);
}
sub generateCall{
my $nodeName = shift;
my $password = $nodeName.$myNode;
my $callHash = md5_hex($password);
my $callPasswd = $nodeName."\t"."U".$myNode."\t".$callHash."\n";
open(FH, '>>', $callFile);
print FH $callPasswd;
}
sub generatePasswd {
my $nodeName = shift;
my $password = $myNode.$nodeName;
my $passwdHash = md5_hex($password);
my $authPasswd = "U".$nodeName."\t".$passwdHash."\n";
open(FH, '>>', $passwdFile);
print FH $authPasswd;
}
sub preCheck()
{
print("Updating git repo\n");
chdir("../");
#system("git pull");
print("Installing authorized_keys\n");
#rename("authorized_keys",".ssh/authorized_keys");
chdir("./utils");
print("Pre-flight checks done\n");
getNodes();
}
sub showHelp()
{
print("Usage: bootstrap.pl [OPTION...] node-id\n");
print("Generate uucp configuration files.\n\n");
print(" -t truncate files before appending config\n");
print(" -h display this help and exit\n");
}
getopts("ht", \%options);
if ($ARGV[0]) {
$myNode=$ARGV[0];
} else {
showHelp();
exit;
}
if ($options{h}) {
showHelp();
exit;
}
preCheck();
print("\n\n");
print("This setup script has finished. Please move the generated configuration files as ROOT to /etc/uucp\n");