2007-10-26 22:44:33 +00:00
#!/usr/bin/env perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
2007-12-11 19:14:43 +00:00
BEGIN
{
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
}
use lib "$::XCATROOT/lib/perl";
2008-01-26 00:23:23 +00:00
use Storable qw(freeze thaw);
2007-12-03 13:10:23 +00:00
use xCAT::Utils;
2008-01-16 21:14:50 +00:00
use File::Path;
2008-03-07 04:13:21 +00:00
use Time::HiRes qw(sleep);
2008-03-10 15:36:44 +00:00
use Thread qw(yield);
2008-01-25 15:07:53 +00:00
use xCAT::Client submit_request;
2008-04-21 15:27:11 +00:00
my $clientselect = new IO::Select;
2008-05-13 17:30:56 +00:00
my $sslclients = 0; #THROTTLE
my $maxsslclients = 16; #default
2007-12-03 13:10:23 +00:00
2008-04-24 17:26:30 +00:00
sub xexit {
2008-04-28 13:47:20 +00:00
while (wait() > 0) {
2008-04-24 17:26:30 +00:00
yield;
}
exit @_;
}
my $dispatch_children=0;
my %dispatched_children=();
my $plugin_numchildren=0;
2008-04-21 20:43:07 +00:00
my %plugin_children;
2007-12-03 13:10:23 +00:00
use IO::Socket::SSL;
2008-03-10 18:05:26 +00:00
my $inet6support;
$inet6support=eval { require Socket6 };
if ($inet6support) {
$inet6support = eval { require IO::Socket::INET6 };
}
if ($inet6support) {
$inet6support = eval { require IO::Socket::SSL::inet6 };
}
unless ($inet6support) {
eval { require Socket };
eval { require IO::Socket::INET };
2007-12-03 13:10:23 +00:00
}
2008-02-01 16:23:20 +00:00
my $dispatch_requests = 1; # govern whether commands are dispatchable
2007-10-26 22:44:33 +00:00
use IO::Socket;
use IO::Handle;
use IO::Select;
use XML::Simple;
2008-04-16 17:03:37 +00:00
if ($^O =~ /^linux/i) {
$XML::Simple::PREFERRED_PARSER='XML::Parser';
}
2007-10-26 22:44:33 +00:00
use xCAT::Table;
use Data::Dumper;
use Getopt::Long;
2008-03-25 20:44:36 +00:00
use Sys::Syslog qw(:DEFAULT setlogsock);
2008-03-25 20:58:10 +00:00
openlog("xcatd",,"local4");
2008-03-25 21:37:15 +00:00
setlogsock(["tcp","unix","stream"]);
2007-10-26 22:44:33 +00:00
use xCAT::NotifHandler;
2007-12-11 19:14:43 +00:00
use xCAT_monitoring::monitorctrl;
2007-11-29 19:22:49 +00:00
2007-10-26 22:44:33 +00:00
Getopt::Long::Configure("bundling");
Getopt::Long::Configure("pass_through");
use Storable qw(dclone);
use POSIX qw(WNOHANG setsid);
use strict;
my $pidfile;
2008-01-27 17:37:24 +00:00
my $foreground;
2007-10-26 22:44:33 +00:00
GetOptions(
2008-01-27 17:37:24 +00:00
'pidfile|p=s' => \$pidfile,
'foreground|f' => \$foreground
2007-10-26 22:44:33 +00:00
);
2008-04-04 18:33:40 +00:00
#start syslog if it is not up
if (xCAT::Utils->isLinux()) {
my $init_file="/etc/init.d/syslog";
if (-f "/etc/fedora-release") {
$init_file="/etc/init.d/rsyslog";
}
my $result=`$init_file status 2>&1`;
if ($result !~ /running/i) {
`$init_file start`;
}
} else {
my $result=`lssrc -s syslogd 2>&1`;
if ($result !~ /active/i) {
`startsrc -s syslogd`;
}
}
2007-10-26 22:44:33 +00:00
my $quit = 0;
my $port;
my $sport;
my $domain;
my $xcatdir;
my $sitetab=xCAT::Table->new('site');
unless ($sitetab) {
print ("ERROR: Unable to open basic site table for configuration\n");
}
2007-12-11 19:14:43 +00:00
2007-10-26 22:44:33 +00:00
my ($tmp) = $sitetab->getAttribs({'key'=>'xcatdport'},'value');
unless ($tmp) {
die "ERROR:Need xcatdport defined in site table, try chtab key=xcatdport site.value=3001";
}
$port = $tmp->{value};
$sport = $tmp->{value}+1;
2007-12-11 19:14:43 +00:00
my $plugins_dir=$::XCATROOT.'/lib/perl/xCAT_plugin';
2007-10-26 22:44:33 +00:00
($tmp) = $sitetab->getAttribs({'key'=>'xcatconfdir'},'value');
2007-11-30 15:24:09 +00:00
$xcatdir = (($tmp and $tmp->{value}) ? $tmp->{value} : "/etc/xcat");
2007-10-26 22:44:33 +00:00
2008-01-20 19:20:46 +00:00
$sitetab->close;
2007-10-26 22:44:33 +00:00
my $progname;
$SIG{PIPE} = sub { die "SIGPIPE $$progname encountered a broken pipe (probably Ctrl-C by client)" };
2008-01-26 00:23:23 +00:00
$progname = \$0;
2007-10-26 22:44:33 +00:00
sub daemonize {
chdir('/');
umask 0;
my $pid;
2008-01-21 19:49:59 +00:00
defined($pid = xCAT::Utils->xfork) or die "Can't fork: $!";
2007-10-26 22:44:33 +00:00
if ($pid) {
if ($pidfile) {
open(PFILE, '>', $pidfile);
print PFILE $pid;
close (PFILE);
} else {
printf ("xCATd starting as PID $pid \n");
}
exit;
}
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>/dev/null';
open STDERR, '>/dev/null';
$0='xcatd';
$progname = \$0;
setsid or die "Can't start new session";
}
my %cmd_handlers;
sub do_installm_service {
#This function servers as a handler for messages from installing nodes
2008-01-04 14:31:45 +00:00
my $socket;
2008-03-10 18:05:26 +00:00
if ($inet6support) {
2008-01-04 14:31:45 +00:00
$socket = IO::Socket::INET6->new(LocalPort=>$sport,
2007-10-26 22:44:33 +00:00
Proto => 'tcp',
ReuseAddr => 1,
Listen => 64);
2008-01-04 14:31:45 +00:00
} else {
$socket = IO::Socket::INET->new(LocalPort=>$sport,
Proto => 'tcp',
ReuseAddr => 1,
Listen => 64);
}
2007-10-26 22:44:33 +00:00
unless ($socket) {
2007-12-11 14:25:55 +00:00
syslog("local4|err","xcatd unable to open install monitor services on $sport");
2007-10-26 22:44:33 +00:00
die;
}
until ($quit) {
$SIG{ALRM} = sub { die "XCATTIMEOUT"; };
my $conn;
next unless $conn = $socket->accept;
2008-01-04 14:31:45 +00:00
my @clients;
2008-03-10 18:05:26 +00:00
if ($inet6support) {
2008-01-04 14:31:45 +00:00
@clients = gethostbyaddr($conn->peeraddr,AF_INET6);
} else {
@clients = gethostbyaddr($conn->peeraddr,AF_INET);
}
2007-11-04 16:06:16 +00:00
my $validclient=0;
2007-10-26 22:44:33 +00:00
my $node;
2007-11-04 16:06:16 +00:00
foreach my $client (@clients) {
$client =~ s/\..*//;
($node) = noderange($client); #ensure this is coming from a node IP at least
if ($node) { #Means the source isn't a valid deal...
$validclient=1;
last;
}
}
unless ($validclient) {
close($conn);
next;
2007-10-26 22:44:33 +00:00
}
2008-01-16 21:14:50 +00:00
my $tftpdir = "/tftpboot/";
2007-10-26 22:44:33 +00:00
eval {
alarm(2);
print $conn "ready\n";
while (my $text = <$conn>) {
alarm(0);
print $conn "done\n";
$text =~ s/\r//g;
if ($text =~ /next/) {
my %request = (
command => [ 'nodeset' ],
node => [ $node ],
arg => [ 'next' ],
);
close($conn);
2008-02-05 15:45:58 +00:00
#node should be blocked, race condition may occur otherwise
#my $pid=xCAT::Utils->xfork();
#unless ($pid) { #fork off the nodeset and potential slowness
2007-10-26 22:44:33 +00:00
plugin_command(\%request,undef,\&convey_response);
2008-02-05 15:45:58 +00:00
# exit(0);
#}
2008-01-16 21:14:50 +00:00
} elsif ($text =~ /^unlocktftpdir/) { #TODO: only nodes in install state should be allowed
2008-02-07 21:48:21 +00:00
mkpath("$tftpdir/xcat/$node");
2008-01-16 21:14:50 +00:00
chmod 01777,"$tftpdir/xcat/$node";
chmod 0666,glob("$tftpdir/xcat/$node/*");
2008-02-07 21:48:21 +00:00
close($conn);
2008-01-16 21:14:50 +00:00
} elsif ($text =~ /locktftpdir/) {
chmod 0755,"$tftpdir/xcat/$node";
chmod 0644,glob("$tftpdir/xcat/$node/*");
2008-02-07 21:48:21 +00:00
} elsif ($text =~ /^setiscsiparms/) {
$text =~ s/^setiscsiparms\s+//;
2008-01-16 21:14:50 +00:00
my $kname;
my $iname;
my $kcmdline;
($kname,$iname,$kcmdline) = split(/\s+/,$text,3);
chomp($kcmdline);
2008-04-25 15:36:56 +00:00
my $bptab = xCAT::Table->new('bootparams',-create=>1);
$bptab->setNodeAttribs($node,{kernel=>"xcat/$node/$kname",initrd=>"xcat/$node/$iname",kcmdline=>$kcmdline});
2008-02-07 21:48:21 +00:00
my $iscsitab = xCAT::Table->new('iscsi',-create=>1);
$iscsitab->setNodeAttribs($node,{kernel=>"xcat/$node/$kname",initrd=>"xcat/$node/$iname",kcmdline=>$kcmdline});
2008-01-16 21:14:50 +00:00
my $chaintab = xCAT::Table->new('chain',-create=>1);
2008-02-07 21:48:21 +00:00
$chaintab->setNodeAttribs($node,{currstate=>'iscsiboot',currchain=>'netboot'});
2008-04-25 15:36:56 +00:00
$bptab->close;
2008-01-16 21:14:50 +00:00
$chaintab->close;
2008-04-25 15:36:56 +00:00
undef $bptab;
2008-01-16 21:14:50 +00:00
undef $chaintab;
my %request = (
command => [ 'nodeset' ],
node => [ $node ],
arg => [ 'enact' ],
);
2008-01-21 19:49:59 +00:00
my $pid=xCAT::Utils->xfork();
2008-01-16 21:14:50 +00:00
unless ($pid) { #fork off the nodeset and potential slowness
plugin_command(\%request,undef,\&convey_response);
2008-04-24 17:26:30 +00:00
xexit(0);
2008-01-16 21:14:50 +00:00
}
}
2007-10-26 22:44:33 +00:00
alarm(2);
}
alarm(0);
};
if ($@) {
if ($@ =~ /XCATTIMEOUT/) {
2007-12-11 14:25:55 +00:00
syslog("local4|err","xcatd installmonitor timed out talking to $node");
2007-10-26 22:44:33 +00:00
} else {
2007-12-11 14:25:55 +00:00
syslog("local4|err","xcatd: possible BUG encountered by xCAT install monitor service: ".$@);
2007-10-26 22:44:33 +00:00
}
}
}
}
sub do_udp_service { #This function opens up a UDP port
#It will do similar to the standard service, except:
#-Obviously, unencrypted and messages are not guaranteed
#-For that reason, more often than not plugins designed with
#-this method will not expect to have a callback
#Also, this throttles to handle one message at a time, so no forking either
#Explicitly, to handle whatever operations nodes periodically send during discover state
#Could be used for heartbeating and such as desired
2008-02-01 16:23:20 +00:00
$dispatch_requests=0;
2008-01-04 14:31:45 +00:00
my $socket;
2008-01-11 20:55:28 +00:00
my $select = new IO::Select;
2008-03-10 18:05:26 +00:00
if ($inet6support) {
2008-01-04 14:31:45 +00:00
$socket = IO::Socket::INET6->new(LocalPort => $port,
2007-11-04 16:06:16 +00:00
Proto => 'udp',
Domain => AF_INET);
2008-01-04 14:31:45 +00:00
} else {
$socket = IO::Socket::INET->new(LocalPort => $port,
Proto => 'udp',
Domain => AF_INET);
}
2007-12-11 14:25:55 +00:00
openlog("xCAT UDP",'','local4');
2007-10-26 22:44:33 +00:00
unless ($socket) {
syslog("err","xCAT UDP service unable to open port $port: $!");
closelog();
die "Unable to start UDP on $port";
}
2008-04-15 18:26:59 +00:00
$select->add($socket);
2007-10-26 22:44:33 +00:00
my $data;
my $part;
my $sport;
my $client;
my $peerhost;
2008-01-11 20:55:28 +00:00
my %packets;
2007-10-26 22:44:33 +00:00
until ($quit) {
2008-01-11 20:55:28 +00:00
eval {
while (1) {
2008-03-11 13:10:58 +00:00
until ($select->can_read(5)) { if ($quit) { last; }; yield; } #Wait for data
2008-01-11 20:55:28 +00:00
while ($select->can_read(0)) { #Pull all buffer data that can be pulled
$part = $socket->recv($data,1500);
($sport,$client) = sockaddr_in($part);
2008-04-11 18:12:10 +00:00
if ($sport < 1000) { #Only remember udp packets from privileged ports
$packets{inet_ntoa($client)} = [$part,$data];
}
2008-01-11 20:55:28 +00:00
}
foreach my $pkey (keys %packets) {
($sport,$client) = sockaddr_in($packets{$pkey}->[0]);
$data=$packets{$pkey}->[1];
2007-10-26 22:44:33 +00:00
$peerhost=gethostbyaddr($client,AF_INET)."\n";
my $req = eval { XMLin($data, SuppressEmpty=>undef,ForceArray=>1) };
if ($req and $req->{command} and ($req->{command}->[0] eq "findme")) {
2008-04-11 18:12:10 +00:00
$req->{'_xcat_clienthost'}=gethostbyaddr($client,AF_INET);
2008-01-25 15:07:53 +00:00
$req->{'_xcat_clientip'}=inet_ntoa($client);
$req->{'_xcat_clientport'}=$sport;
2007-10-26 22:44:33 +00:00
if (defined($cmd_handlers{"findme"})) {
2008-01-11 20:55:28 +00:00
$req->{cacheonly}->[0] = 1;
2007-10-26 22:44:33 +00:00
plugin_command($req,undef,\&convey_response);
2008-01-11 20:55:28 +00:00
if ($req->{cacheonly}->[0]) {
delete $req->{cacheonly};
plugin_command($req,undef,\&convey_response);
2008-03-21 20:00:07 +00:00
#if ($req) {
# $req->{cacheonly}->[0] = 1;
# $req->{checkallmacs}->[0] = 1;
# plugin_command($req,undef,\&convey_response);
# }
2008-01-11 20:55:28 +00:00
}
2007-10-26 22:44:33 +00:00
}
}
if ($quit) { last; }
2008-01-11 20:55:28 +00:00
while ($select->can_read(0)) { #grab any incoming requests during run
$part = $socket->recv($data,1500);
($sport,$client) = sockaddr_in($part);
$packets{inet_ntoa($client)} = [$part,$data];
}
#Some of those 'future' packets might be stale dupes of this packet, so...
delete $packets{$pkey}; #Delete any duplicates of current packet
2007-10-26 22:44:33 +00:00
}
2008-01-11 20:55:28 +00:00
if ($quit) { last; }
}
2007-10-26 22:44:33 +00:00
};
if ($@) {
2007-12-11 14:25:55 +00:00
syslog("local4|err","xcatd: possible BUG encountered by xCAT UDP service: ".$@);
2007-10-26 22:44:33 +00:00
}
}
}
sub scan_plugins {
my @plugins=glob($plugins_dir."/*.pm");
foreach (@plugins) {
/.*\/([^\/]*).pm$/;
my $modname = $1;
require "$_";
no strict 'refs';
my $cmd_adds=${"xCAT_plugin::".$modname."::"}{handled_commands}->();
foreach (keys %$cmd_adds) {
my $value = $_;
if (defined($cmd_handlers{$_})) {
my $add=1;
#This next bit of code iterates through the handlers.
#If the value doesn't contain an equal, and has an equivalent entry added by
# another plugin already, don't add (otherwise would hit the DB multiple times)
# a better idea, restructure the cmd_handlers as a multi-level hash
# prove out this idea real quick before doing that
foreach (@{$cmd_handlers{$_}}) {
if (($_->[1] eq $cmd_adds->{$value}) and (($cmd_adds->{$value} !~ /=/) or ($_->[0] eq $modname))) {
$add = 0;
}
}
if ($add) { push @{$cmd_handlers{$_}},[$modname,$cmd_adds->{$_}]; }
#die "Conflicting handler information from $modname";
} else {
$cmd_handlers{$_} = [ [$modname,$cmd_adds->{$_}] ];
}
}
}
}
scan_plugins;
2008-03-25 20:44:36 +00:00
eval {
syslog("local4|info","xCATd: service starting");
};
if ($@) {
print "ERROR: $@";
2008-04-24 17:26:30 +00:00
xexit;
2008-03-25 20:44:36 +00:00
}
2008-01-27 17:37:24 +00:00
unless ($foreground) {
daemonize;
}
2008-04-24 17:26:30 +00:00
my $CHILDPID=0; #Global for reapers
sub generic_reaper {
2008-04-28 13:47:20 +00:00
while (waitpid(-1,WNOHANG) > 0) {
yield;
}
2008-04-24 17:26:30 +00:00
$SIG{CHLD} = \&generic_reaper;
}
2008-05-08 15:22:22 +00:00
#sub ssl_reaper {
# while (waitpid(-1,WNOHANG) > 0) {
# $sslclients--;
# }
# $SIG{CHLD} = \&ssl_reaper;
#}
2008-04-24 17:26:30 +00:00
sub dispatch_reaper {
while (($CHILDPID =waitpid(-1, WNOHANG)) > 0) {
if ($dispatched_children{$CHILDPID}) {
delete $dispatched_children{$CHILDPID};
$dispatch_children--;
}
}
$SIG{CHLD} = \&dispatch_reaper;
}
sub plugin_reaper {
while (($CHILDPID = waitpid(-1, WNOHANG)) > 0) {
if ($plugin_children{$CHILDPID}) {
delete $plugin_children{$CHILDPID};
$plugin_numchildren--;
}
}
$SIG{CHLD} = \&plugin_reaper;
}
$SIG{CHLD} = \&generic_reaper;
2008-04-18 16:12:52 +00:00
$SIG{TERM} = $SIG{INT} = sub {
printf("Asked to quit...\n");
$quit++;
2008-04-21 18:31:27 +00:00
foreach (keys %dispatched_children) {
kill 2, $_;
}
2008-04-24 17:26:30 +00:00
$SIG{ALRM} = sub { xexit 0; }; #die "Did not close out in time for 5 second grace period"; };
2008-04-21 15:27:11 +00:00
alarm(2);
2008-04-18 16:12:52 +00:00
};
2007-10-26 22:44:33 +00:00
2008-01-21 19:49:59 +00:00
my $pid = xCAT::Utils->xfork;
2007-10-26 22:44:33 +00:00
defined $pid or die "Unable to fork for UDP/TCP";
unless ($pid) {
$$progname="xcatd: UDP listener";
do_udp_service;
2008-04-24 17:26:30 +00:00
xexit(0);
2007-10-26 22:44:33 +00:00
}
2008-01-21 19:49:59 +00:00
$pid = xCAT::Utils->xfork;
2007-10-26 22:44:33 +00:00
defined $pid or die "Unable to fork installmonitor";
unless ($pid) {
$$progname="xcatd: install monitor";
do_installm_service;
2008-04-24 17:26:30 +00:00
xexit(0);
2007-10-26 22:44:33 +00:00
}
$$progname="xcatd: SSL listener";
2007-12-11 14:25:55 +00:00
openlog("xCAT SSL","","local4");
2008-03-31 14:21:16 +00:00
my $listener = IO::Socket::INET->new(
2007-10-26 22:44:33 +00:00
LocalPort => $port,
Listen => 64,
Reuse => 1,
);
unless ($listener) {
2008-04-21 15:27:11 +00:00
kill 2, $pid;
2007-10-26 22:44:33 +00:00
syslog("err","xCAT service unable to open SSL services on $port: $!");
closelog();
die "ERROR:Unable to start xCAT service on port $port.";
}
closelog();
#setup signal in NotifHandler so that the cache can be updated
xCAT::NotifHandler::setup($$);
2007-11-29 19:22:49 +00:00
#start the monitoring process
xCAT_monitoring::monitorctrl::start($$);
2007-10-26 22:44:33 +00:00
my $peername;
2008-04-18 18:52:45 +00:00
my $ssltimeout;
2008-05-13 17:30:56 +00:00
$SIG{CHLD} = \&ssl_reaper; #THROTTLE
2007-10-26 22:44:33 +00:00
until ($quit) {
2008-03-31 14:21:16 +00:00
next unless my $cnnection=$listener->accept;
my $connection;
2008-05-13 17:30:56 +00:00
while ($sslclients > $maxsslclients) { #THROTTLE
yield;
}
2008-01-21 19:49:59 +00:00
my $child = xCAT::Utils->xfork(); #Yes we fork, IO::Socket::SSL is not threadsafe..
2007-10-26 22:44:33 +00:00
unless (defined $child) {
die "xCATd cannot fork";
}
if ($child == 0) {
2008-05-13 17:30:56 +00:00
$SIG{CHLD} = \&generic_reaper; #THROTTLE
2007-10-26 22:44:33 +00:00
$listener->close;
2008-04-18 18:52:45 +00:00
$SIG{ALRM} = sub { $ssltimeout = 1; die; };
eval {
alarm(10);
$connection = IO::Socket::SSL->start_SSL($cnnection,
2008-05-02 19:38:44 +00:00
SSL_key_file=>$xcatdir."/cert/server-cred.pem",
SSL_cert_file=>$xcatdir."/cert/server-cred.pem",
2008-04-18 18:52:45 +00:00
SSL_ca_file=>$xcatdir."/cert/ca.pem",
SSL_server=>1,
SSL_verify_mode=> 1
);
alarm(0);
};
undef $SIG{ALRM};
if ($@) { #SSL failure
close($cnnection);
2008-04-24 17:26:30 +00:00
xexit 0;
2008-04-18 18:52:45 +00:00
}
unless ($connection) {
2008-04-24 17:26:30 +00:00
xexit 0;
2008-04-18 18:52:45 +00:00
}
2008-04-21 15:27:11 +00:00
$clientselect->add($connection);
2007-10-26 22:44:33 +00:00
my $peerhost=undef;
my $peer=$connection->peer_certificate("owner");
if ($peer) {
$peer =~ m/CN=([^\/]*)/;
$peername = $1;
} else {
$peername=undef;
}
2008-01-20 19:20:46 +00:00
$sitetab=xCAT::Table->new('site');
2007-10-26 22:44:33 +00:00
my ($tmp) = $sitetab->getAttribs({'key'=>'domain'},'value');
if (defined $tmp->{value}) {
$domain = $tmp->{value};
}
2008-01-20 19:20:46 +00:00
$sitetab->close;
2008-01-04 14:31:45 +00:00
2008-03-10 18:05:26 +00:00
if ($inet6support) {
2007-11-08 15:35:48 +00:00
$peerhost = gethostbyaddr($connection->peeraddr,AF_INET6);
2008-01-04 14:31:45 +00:00
} else {
$peerhost = gethostbyaddr($connection->peeraddr,AF_INET);
}
2008-01-03 15:55:47 +00:00
unless ($peerhost) { $peerhost = gethostbyaddr($connection->peeraddr,AF_INET); }
2007-10-26 22:44:33 +00:00
$peerhost =~ s/\.$domain\.*$//;
$peerhost =~ s/-eth\d*$//;
$peerhost =~ s/-myri\d*$//;
$peerhost =~ s/-ib\d*$//;
#printf('info'.": xcatd: connection from ".($peername ? $peername . "@" . $peerhost : $peerhost)."\n");
$$progname="xCATd SSL: Instance for ".($peername ? $peername ."@".$peerhost : $peerhost);
service_connection($connection,$peername,$peerhost);
2008-04-24 17:26:30 +00:00
xexit(0);
2007-10-26 22:44:33 +00:00
}
2008-05-13 17:30:56 +00:00
$sslclients++; #THROTTLE
2008-03-31 14:21:16 +00:00
$cnnection->close();
2007-10-26 22:44:33 +00:00
}
$listener->close;
2007-11-29 19:22:49 +00:00
#stop the monitoring process
xCAT_monitoring::monitorctrl::stop($$);
2007-10-26 22:44:33 +00:00
my $parent_fd;
my %resps;
sub plugin_command {
my $req = shift;
my $sock = shift;
my $callback = shift;
my %handler_hash;
use xCAT::NodeRange;
$Main::resps={};
my @nodes;
if ($req->{node}) {
@nodes = @{$req->{node}};
2008-03-05 16:57:27 +00:00
} elsif ($req->{noderange} and $req->{noderange}->[0]) {
2007-10-26 22:44:33 +00:00
@nodes = noderange($req->{noderange}->[0]);
if (nodesmissed) {
my $rsp = {errorcode=>1,error=>"Invalid nodes in noderange:".join(',',nodesmissed)};
2008-05-02 12:53:09 +00:00
$rsp->{serverdone} = {};
2007-10-26 22:44:33 +00:00
if ($sock) {
print $sock XMLout($rsp,RootName=>'xcatresponse' ,NoAttr=>1);
}
return ($rsp);
}
2008-03-05 16:31:35 +00:00
unless (@nodes) {
$req->{emptynoderange} = [1];
}
2007-10-26 22:44:33 +00:00
}
if (@nodes) { $req->{node} = \@nodes; }
2008-02-28 04:52:08 +00:00
my %unhandled_nodes;
foreach (@nodes) {
$unhandled_nodes{$_}=1;
}
my $useunhandled=0;
2007-10-26 22:44:33 +00:00
if (defined($cmd_handlers{$req->{command}->[0]})) {
my $hdlspec;
foreach (@{$cmd_handlers{$req->{command}->[0]}}) {
$hdlspec =$_->[1];
my $ownmod = $_->[0];
if ($hdlspec =~ /:/) { #Specificed a table lookup path for plugin name
2008-02-28 04:52:08 +00:00
$useunhandled=1;
2007-10-26 22:44:33 +00:00
my $table;
my $cols;
($table,$cols) = split(/:/,$hdlspec);
my @colmns=split(/,/,$cols);
my @columns;
my $hdlrtable=xCAT::Table->new($table);
unless ($hdlrtable) {
#TODO: proper error handling
}
my $node;
my $colvals = {};
foreach my $colu (@colmns) {
if ($colu =~ /=/) { #a value redirect to a pattern/specific name
my $coln; my $colv;
($coln,$colv) = split(/=/,$colu,2);
$colvals->{$coln} = $colv;
push (@columns,$coln);
} else {
push (@columns,$colu);
}
}
unless (@nodes) { #register the plugin in the event of usage
$handler_hash{$ownmod} = 1;
}
foreach $node (@nodes) {
my $attribs = $hdlrtable->getNodeAttribs($node,\@columns);
unless (defined($attribs)) { next; } #TODO: This really ought to craft an unsupported response for this request
foreach (@columns) {
my $col=$_;
if (defined($attribs->{$col})) {
if ($colvals->{$col}) { #A pattern match style request.
if ($attribs->{$col} =~ /$colvals->{$col}/) {
$handler_hash{$ownmod}->{$node} = 1;
2008-02-28 04:52:08 +00:00
delete $unhandled_nodes{$node};
2007-10-26 22:44:33 +00:00
last;
}
} else {
$handler_hash{$attribs->{$col}}->{$node} = 1;
2008-02-28 04:52:08 +00:00
delete $unhandled_nodes{$node};
2007-10-26 22:44:33 +00:00
last;
}
}
}
}
2008-01-20 19:20:46 +00:00
$hdlrtable->close;
2007-10-26 22:44:33 +00:00
} else {
unless (@nodes) {
$handler_hash{$hdlspec} = 1;
}
foreach (@nodes) { #Specified a specific plugin, not a table lookup
$handler_hash{$hdlspec}->{$_} = 1;
}
}
}
} else {
return 1; #TODO: error back that request has no known plugin for it
}
2008-02-28 04:52:08 +00:00
if ($useunhandled) {
my $queuelist;
foreach (@{$cmd_handlers{$req->{command}->[0]}->[0]}) {
unless (/:/) {
next;
}
$queuelist .= "$_,";
}
$queuelist =~ s/,$//;
$queuelist =~ s/:/./g;
foreach (keys %unhandled_nodes) {
if ($sock) {
print $sock XMLout({node=>[{name=>[$_],data=>["Unable to identify plugin for this command, check relevant tables: $queuelist"],errorcode=>[1]}]},NoAttr=>1,RootName=>'xcatresponse');
} else {
$callback->({node=>[{name=>[$_],data=>['Unable to identify plugin for this command, check relevant tables'],errorcode=>[1]}]});
}
}
}
2008-04-24 17:26:30 +00:00
$plugin_numchildren=0;
2008-04-21 20:43:07 +00:00
%plugin_children=();
2008-04-24 17:26:30 +00:00
$SIG{CHLD} = \&plugin_reaper; #sub {my $plugpid; while (($plugpid = waitpid(-1, WNOHANG)) > 0) { if ($plugin_children{$plugpid}) { delete $plugin_children{$plugpid}; $plugin_numchildren--; } } };
2007-10-26 22:44:33 +00:00
my $check_fds;
if ($sock) {
$check_fds = new IO::Select;
}
foreach (keys %handler_hash) {
my $modname = $_;
if (-r $plugins_dir."/".$modname.".pm") {
require $plugins_dir."/".$modname.".pm";
2008-04-24 17:26:30 +00:00
$plugin_numchildren++;
2007-10-26 22:44:33 +00:00
my $pfd; #will be referenced for inter-process messaging.
2008-01-27 17:37:24 +00:00
my $parfd; #not causing a problem that I discern yet, but theoretically
2007-10-26 22:44:33 +00:00
my $child;
if ($sock) { #If $sock not passed in, don't fork..
2008-01-27 17:37:24 +00:00
socketpair($pfd, $parfd,AF_UNIX,SOCK_STREAM,PF_UNSPEC) or die "socketpair: $!";
2007-10-26 22:44:33 +00:00
#pipe($pfd,$cfd);
2008-01-27 17:37:24 +00:00
$parfd->autoflush(1);
2007-10-26 22:44:33 +00:00
$pfd->autoflush(1);
2008-01-21 19:49:59 +00:00
$child = xCAT::Utils->xfork;
2007-10-26 22:44:33 +00:00
} else {
$child = 0;
}
unless (defined $child) { die "Fork failed"; }
if ($child == 0) {
2008-01-27 17:37:24 +00:00
$parent_fd = $parfd;
2007-10-26 22:44:33 +00:00
my $oldprogname=$$progname;
$$progname=$oldprogname.": $modname instance";
if ($sock) { close $pfd; }
unless ($handler_hash{$_} == 1) {
my @nodes = sort {($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0] || $a cmp $b } (keys %{$handler_hash{$_}});
$req->{node}=\@nodes;
}
no strict "refs";
2008-02-01 16:23:20 +00:00
if ($dispatch_requests) {
dispatch_request($req,$callback,$modname);
} else {
2008-02-15 14:54:46 +00:00
undef $SIG{CHLD};
2008-02-01 16:23:20 +00:00
${"xCAT_plugin::".$modname."::"}{process_request}->($req,$callback,\&do_request);
}
2007-10-26 22:44:33 +00:00
$$progname=$oldprogname;
if ($sock) {
close($parent_fd);
2008-04-24 17:26:30 +00:00
xexit(0);
2007-10-26 22:44:33 +00:00
}
} else {
2008-04-21 20:43:07 +00:00
$plugin_children{$child}=1;
2008-01-27 17:37:24 +00:00
close $parfd;
2007-10-26 22:44:33 +00:00
$check_fds->add($pfd);
}
}
}
unless ($sock) { return $Main::resps };
2008-04-24 17:26:30 +00:00
while (($plugin_numchildren > 0) and ($check_fds->count > 0)) { #this tracks end of useful data from children much more closely
2007-10-26 22:44:33 +00:00
relay_fds($check_fds,$sock);
}
#while (relay_fds($check_fds,$sock)) {}
my %done;
$done{serverdone} = {};
if ($req->{transid}) {
$done{transid}=$req->{transid}->[0];
}
if ($sock) { print $sock XMLout(\%done,RootName => 'xcatresponse',NoAttr=>1); }
}
2008-01-26 00:23:23 +00:00
my $dispatch_dnf=0;
2008-01-25 16:17:19 +00:00
my $dispatch_cb;
2008-01-26 00:23:23 +00:00
my $dispatch_parentfd;
2008-01-25 16:17:19 +00:00
sub dispatch_callback {
2008-01-26 00:23:23 +00:00
my $rspo = shift;
2008-02-05 15:45:58 +00:00
unless ($rspo) {
return;
}
2008-01-26 00:23:23 +00:00
my $rsp = {%$rspo}; # deep copy
2008-01-25 16:17:19 +00:00
delete $rsp->{serverdone};
2008-01-26 00:23:23 +00:00
unless (%$rsp) { return; }
if ($dispatch_dnf) {
$dispatch_cb->($rsp);
} else {
print $dispatch_parentfd freeze($rsp);
print $dispatch_parentfd "\nENDOFFREEZE6sK6xa\n";
2008-03-11 13:10:58 +00:00
yield; #This has to happen before next line could possibly work anyway
2008-04-18 19:15:26 +00:00
my $parselect = new IO::Select;
$parselect->add($dispatch_parentfd);
2008-04-21 15:27:11 +00:00
my $rsp;
if ($parselect->can_read(5)) { #block for up to 5 seconds before continuing
if ($rsp = <$dispatch_parentfd>) {
2008-04-21 18:31:27 +00:00
if ($rsp =~ /die/ or $quit) {
2008-04-24 17:26:30 +00:00
xexit 0;
2008-04-21 15:27:11 +00:00
}
} else {
$parselect->remove($dispatch_parentfd); #Block until parent acks data
}
}
2008-01-26 00:23:23 +00:00
}
2008-01-25 16:17:19 +00:00
}
2008-01-26 00:23:23 +00:00
sub relay_dispatch {
my $fds = shift;
2008-03-07 04:13:21 +00:00
my @ready_ins = $fds->can_read(1);
2008-01-26 00:23:23 +00:00
foreach my $rin (@ready_ins) {
my $data;
if ($data = <$rin>) {
while ($data !~ /ENDOFFREEZE6sK6xa/) {
$data .= <$rin>;
}
my $response = thaw($data);
2008-04-21 15:27:11 +00:00
print $rin "dfin\n";
2008-01-26 00:23:23 +00:00
$dispatch_cb->($response);
} else {
$fds->remove($rin);
close($rin);
}
}
2008-04-21 15:27:11 +00:00
foreach my $rin ($clientselect->can_read(0)) {
my $subselect = new IO::Select;
$subselect->add($rin);
my $clientintr="";
my $subdata;
while ($subselect->can_read(1)) {
if ($subdata=<$rin>) {
$clientintr.=$subdata;
} else {
$subselect->remove($rin);
close($rin);
}
}
2008-04-21 18:48:07 +00:00
unless ($clientintr) {
next;
}
2008-04-21 15:27:11 +00:00
$clientintr=XMLin($clientintr, SuppressEmpty=>undef,ForceArray=>1 );
if ($clientintr->{abortcommand}->[0]) {
2008-04-21 18:31:27 +00:00
foreach (keys %dispatched_children) {
kill 2, $_;
}
2008-04-21 15:27:11 +00:00
foreach my $cin ($fds->handles) {
print $cin "die\n";
$fds->remove($cin);
close($cin);
}
die "Client abort requested";
}
}
2008-03-10 15:36:44 +00:00
yield; #At this point, explicitly yield to other processes. If children will have more data, this process would otherwise uselessly loop on data that never will be. If children are all done, still no harm in waiting a short bit for a timeslice to come back
2008-01-26 00:23:23 +00:00
return scalar(@ready_ins);
}
2008-01-24 13:37:34 +00:00
sub dispatch_request {
2008-04-21 20:43:07 +00:00
%dispatched_children=();
2008-01-26 00:23:23 +00:00
my $req = shift;
$dispatch_cb = shift;
my $modname = shift;
my $reqs = [];
my $child_fdset = new IO::Select;
no strict "refs";
#Hierarchy support. Originally, the default scope for noderange commands was
#going to be the servicenode associated unless overriden.
#However, assume for example that you have blades and a blade is the service node
#rpower being executed by the servicenode for one of its subnodes would have to
#reach it's own management module. This has the potential to be non-trivial for some quite possible network configurations.
#Since plugins may commonly experience this, a preprocess_request implementation
#will for now be required for a command to be scaled through service nodes
#If the plugin offers a preprocess method, use it to set the request array
if (defined(${"xCAT_plugin::".$modname."::"}{preprocess_request})) {
2008-04-24 18:08:41 +00:00
undef $SIG{CHLD};
2008-01-26 17:55:34 +00:00
$reqs = ${"xCAT_plugin::".$modname."::"}{preprocess_request}->($req,$dispatch_cb,\&do_request);
2008-01-26 00:23:23 +00:00
} else { #otherwise, pass it in without hierarchy support
2008-01-25 15:07:53 +00:00
$reqs = [$req];
2008-01-26 00:23:23 +00:00
}
2008-04-24 17:26:30 +00:00
$dispatch_children=0;
$SIG{CHLD} = \&dispatch_reaper; #sub {my $cpid; while (($cpid =waitpid(-1, WNOHANG)) > 0) { if ($dispatched_children{$cpid}) { delete $dispatched_children{$cpid}; $dispatch_children--; } } };
2008-01-26 00:23:23 +00:00
foreach (@{$reqs}) {
my $pfd;
2008-01-27 17:37:24 +00:00
my $parfd; #use a private variable so it won't trounce itself recursively
2008-01-26 00:23:23 +00:00
my $child;
delete $_->{noderange};
2008-01-27 17:37:24 +00:00
socketpair($pfd, $parfd,AF_UNIX,SOCK_STREAM,PF_UNSPEC) or die "socketpair: $!";
$parfd->autoflush(1);
2008-01-26 00:23:23 +00:00
$pfd->autoflush(1);
$child = xCAT::Utils->xfork;
if ($child) {
2008-04-24 17:26:30 +00:00
$dispatch_children++;
2008-04-21 15:27:11 +00:00
$dispatched_children{$child}=1;
2008-01-26 00:23:23 +00:00
$child_fdset->add($pfd);
next;
}
unless (defined $child) {
$dispatch_cb->({error=>['Fork failure dispatching request'],errorcode=>[1]});
}
2008-04-21 20:43:07 +00:00
undef $SIG{CHLD};
2008-04-21 15:27:11 +00:00
$dispatch_parentfd = $parfd;
2008-01-25 15:07:53 +00:00
if ($_->{'_xcatdest'} and thishostisnot($_->{'_xcatdest'})) {
$ENV{XCATHOST} = ( $_->{'_xcatdest'} =~ /:/ ? $_->{'_xcatdest'} : $_->{'_xcatdest'}.":3001" );
2008-04-21 15:27:11 +00:00
$$progname.=": connection to ".$ENV{XCATHOST};
2008-01-25 15:07:53 +00:00
eval {
undef $_->{'_xcatdest'};
2008-05-02 21:13:31 +00:00
xCAT::Client::submit_request($_,\&dispatch_callback,$xcatdir."/cert/server-cred.pem",$xcatdir."/cert/server-cred.pem",$xcatdir."/cert/ca.pem");
2008-01-25 15:07:53 +00:00
};
if ($@) {
2008-01-26 00:23:23 +00:00
dispatch_callback({error=>["Error dispatching command to ".$ENV{XCATHOST}.""],errorcode=>[1]});
2008-01-25 15:07:53 +00:00
syslog("local4|err","Error dispatching request: ".$@);
}
} else {
2008-04-21 15:27:11 +00:00
$$progname.=": locally executing";
2008-02-15 14:54:46 +00:00
undef $SIG{CHLD};
2008-01-26 00:23:23 +00:00
${"xCAT_plugin::".$modname."::"}{process_request}->($_,\&dispatch_callback,\&do_request);
2008-01-25 15:07:53 +00:00
}
2008-04-24 17:26:30 +00:00
xexit;
2008-01-25 15:07:53 +00:00
}
2008-04-24 17:26:30 +00:00
while (($dispatch_children > 0) and ($child_fdset->count > 0)) { relay_dispatch($child_fdset) }
2008-01-26 00:23:23 +00:00
while (relay_dispatch($child_fdset)) { } #Potentially useless drain.
2008-01-24 13:37:34 +00:00
}
2008-01-25 15:07:53 +00:00
sub thishostisnot {
my $comparison = shift;
my @ips = split /\n/,`/sbin/ip addr`;
my $comp=inet_aton($comparison);
foreach (@ips) {
if (/^\s*inet/) {
my @ents = split(/\s+/);
my $ip=$ents[2];
$ip =~ s/\/.*//;
if (inet_aton($ip) eq $comp) {
return 0;
}
#print Dumper(inet_aton($ip));
}
}
return 1;
}
2007-10-26 22:44:33 +00:00
sub do_request {
my $req = shift;
my $second = shift;
my $rsphandler = \&build_response;
my $sock = undef;
if ($second) {
if (ref($second) eq "CODE") {
$rsphandler = $second;
} elsif (ref($second) eq "GLOB") {
$sock = $second;
}
}
#my $sock = shift; #If no sock, will return a response hash
if ($cmd_handlers{$req->{command}->[0]}) {
return plugin_command($req,$sock,$rsphandler);
} elsif ($req->{command}->[0] eq "noderange" and $req->{noderange}) {
my @nodes = noderange($req->{noderange}->[0]);
my %resp;
if (nodesmissed) {
$resp{warning}="Invalid nodes in noderange:".join ',',nodesmissed;
}
$resp{serverdone} = {};
@{$resp{node}}=@nodes;
if ($req->{transid}) {
$resp{transid}=$req->{transid}->[0];
}
if ($sock) {
print $sock XMLout(\%resp,RootName => 'xcatresponse',NoAttr=>1);
} else {
return (\%resp);
}
} else {
my %resp=(error=>"Unsupported request");
$resp{serverdone} = {};
if ($req->{transid}) {
$resp{transid}=$req->{transid}->[0];
}
if ($sock) {
print $sock XMLout(\%resp,RootName => 'xcatresponse',NoAttr=>1);
} else {
return (\%resp);
}
}
}
sub convey_response {
my $resp=shift;
#TODO: This is where the following will/may happen:
#-Track transaction id
#-Save output for deferred commands
unless ($parent_fd) {
build_response($resp);
return;
}
print $parent_fd XMLout($resp,KeyAttr=>[], NoAttr=>1,RootName=>'xcatresponse');
2008-04-21 15:27:11 +00:00
my $parsel = new IO::Select;
$parsel->add($parent_fd);
if ($parsel->can_read(5)) { #block up to five seconds for parent to ack
2008-04-25 21:09:18 +00:00
<$parent_fd>;
2008-04-21 15:27:11 +00:00
}
#<$parent_fd>; #Block until parent acks data
2007-10-26 22:44:33 +00:00
# KeyAttr => [], NoAttr => 1)
}
sub build_response {
# Handle responses from do_request calls made directly from a plugin
# Merge this response into the full response hash. We'll collect all
# the responses and ship it back on the return to the plugin.
# Note: Need to create a new "deep clone" copy of each response structure
# otherwise the next call will overwrite the reference we pushed on
# the response array
my $resp = shift;
foreach (keys %$resp) {
my $subresp = dclone($resp->{$_});
push (@{$Main::resps->{$_}}, @{$subresp});
}
}
sub service_connection {
my $sock = shift;
my $peername = shift;
my $peerhost = shift;
my $peerport = $sock->peerport;
my %tables=();
#some paranoid measures could reduce a third party abusing stage3 image to attempting to get USER/PASS for BMCs:
# -Well, minimally, ignore requests if requesting node is not in spconfig mode (stage3)
# -Option to generate a random password per 'getipmi' request. This reduces the exposure to a D.O.S. hopefully
#Give only 15 seconds of silence allowed or terminate connection. Using alarm since we are in thread-unsafe world anyway
my $timedout = 0;
$SIG{ALRM} = sub { $timedout = 1; die; };
eval {
my $request;
my $req=undef;
alarm(15);
while (<$sock>) {
alarm(0);
$request .= $_;
#$req = eval { XMLin($request, ForceArray => [ 'attribute' , 'attributepair' ]) };
if ($request =~ m/<\/xcatrequest>/) {
$req = eval { XMLin($request, SuppressEmpty=>undef,ForceArray=>1) };
#we have a full request..
#printf $request."\n";
$request="";
if (validate($peername,$peerhost,$req)) {
2008-01-25 15:07:53 +00:00
$req->{'_xcat_authname'} = [$peername];
$req->{'_xcat_clienthost'} = [$peerhost];
$req->{'_xcat_clientport'}= [$peerport];
2007-10-26 22:44:33 +00:00
$$progname="xCATd SSL: ".$req->{command}->[0]." for ".($peername ? $peername ."@".$peerhost : $peerhost);
if ($cmd_handlers{$req->{command}->[0]}) {
return plugin_command($req,$sock,\&convey_response);
} elsif ($req->{command}->[0] eq "noderange" and $req->{noderange}) {
my @nodes = noderange($req->{noderange}->[0]);
my %resp;
if (nodesmissed) {
$resp{warning}="Invalid nodes in noderange:".join ',',nodesmissed;
}
$resp{serverdone} = {};
@{$resp{node}}=@nodes;
if ($req->{transid}) {
$resp{transid}=$req->{transid}->[0];
}
print $sock XMLout(\%resp,RootName => 'xcatresponse',NoAttr=>1);
next;
} else {
my %resp=(error=>"Unsupported request");
$resp{serverdone} = {};
if ($req->{transid}) {
$resp{transid}=$req->{transid}->[0];
}
print $sock XMLout(\%resp,RootName => 'xcatresponse',NoAttr=>1);
next;
}
} else {
my %resp=(error=>"Permission denied for request");
$resp{serverdone} = {};
if ($req->{transid}) {
$resp{transid}=$req->{transid}->[0];
}
my $response=XMLout(\%resp,RootName =>'xcatresponse',NoAttr => 1);
print $sock $response;
next;
}
}
alarm(15);
}
};
if ($@) { # The eval statement caught a program bug..
2008-04-21 18:48:07 +00:00
if ($@ =~ /^SIGPIPE/) {
2007-12-11 14:25:55 +00:00
syslog("local4|info","xcatd: Unexpected client disconnect");
2008-04-21 18:48:07 +00:00
} elsif ($@ =~ /abort/) {
} else {
syslog("local4|err","xcatd: possible BUG encountered by xCAT TCP service: ".$@);
2007-10-26 22:44:33 +00:00
}
}
2008-04-21 15:27:11 +00:00
$SIG{ALRM}= sub { die "Failure shutting down" };
alarm(5);
2007-10-26 22:44:33 +00:00
foreach (keys %tables) {
$tables{$_}->commit;
}
2008-04-21 15:27:11 +00:00
$sock->close(SSL_fast_shutdown=>1);
2007-10-26 22:44:33 +00:00
if ($timedout == 1) {
printf ("Client timeout");
}
}
sub relay_fds { #Relays file descriptors from pipes to children to the SSL socket
my $fds = shift;
my $sock = shift;
unless ($sock) { return 0; }
my $collate = ( scalar @_ > 0 ? shift : 0);
my @readyset = $fds->can_read(1);
my $rfh;
my $rc = @readyset;
my $text;
foreach $rfh (@readyset) { #go through each child, extract a complete, atomic message
my $line;
while ($line = <$rfh>) { #Will break on complete </xcatresponse> messages, avoid interleave
print $sock $line;
if ($line =~ /<\/xcatresponse>/) {
last;
}
}
if ($line) {
2008-04-21 15:27:11 +00:00
print $rfh "nfin\n"; #Notify convey_response message done
2007-10-26 22:44:33 +00:00
} else {
$fds->remove($rfh);
close($rfh);
}
}
2008-03-10 15:36:44 +00:00
yield; #Give other processes, including children, explicit control, to avoid uselessly aggressive looping
2007-10-26 22:44:33 +00:00
return $rc;
}
sub validate {
#BIG TODO, make this do something meaningful
#here is where we check if $peername is allowed to do $request. $peername if set signifies client has a
#cert that the xCAT CA accepted. This will be a policy table with $peername as key
#things like 'stage2/stage3' and install images will have no client certificate.
#A client key for something that a third party could easily tftp down themselves means nothing
#however, privacy between the nodes can be maintained, and $peerhost will be checked just like 1.2.0.
# returns 1 if policy engine allows the action, 0 if denied
my $peername=shift;
my $peerhost=shift;
my $request=shift;
my $policytable = xCAT::Table->new('policy');
unless ($policytable) {
syslog("err","Unable to open policy data, denying");
return 0;
}
my @policies = $policytable->getTable;
2008-01-20 19:20:46 +00:00
$policytable->close;
2007-10-26 22:44:33 +00:00
my $rule;
foreach $rule (@policies) {
if ($rule->{name} and $rule->{name} ne '*') {
#TODO: more complex matching (lists, wildcards)
next unless ($peername eq $rule->{name});
}
if ($rule->{time} and $rule->{time} ne '*') {
#TODO: time ranges
}
if ($rule->{host} and $rule->{host} ne '*') {
#TODO: more complex matching (lists, noderanges?, wildcards)
next unless ($peerhost eq $rule->{host});
}
if ($rule->{commands} and $rule->{commands} ne '*') {
#TODO: syntax for multiple commands
next unless ($request->{command}->[0] eq $rule->{commands});
}
if ($rule->{parameters} and $rule->{parameters} ne '*') {
2008-03-28 15:55:41 +00:00
my $parms;
if ($request->{arg}) {
$parms = join(' ',@{$request->{arg}});
} else {
$parms = "";
}
my $patt = $rule->{parameters};
unless ($parms =~ /$patt/) {
next;
}
2007-10-26 22:44:33 +00:00
}
if ($rule->{noderange} and $rule->{noderange} ne '*') {
2008-03-28 15:55:41 +00:00
#TODO: not ignore this field
2007-10-26 22:44:33 +00:00
}
# If we are still in, that means this rule is the first match and dictates behavior.
if ($rule->{rule}) {
if ($rule->{rule} =~ /allow/i or $rule->{rule} =~ /accept/i) {
my $logst = "xCAT: Allowing ".$request->{command}->[0];
if ($peername) { $logst .= " for " . $peername };
syslog("authpriv|info",$logst);
return 1;
} else {
my $logst = "xCAT: Denying ".$request->{command}->[0];
if ($peername) { $logst .= " for " . $peername };
syslog("authpriv|info",$logst);
return 0;
}
} else { #Shouldn't be possible....
syslog("err","Impossible line in xcatd reached");
return 0;
}
}
#Reached end of policy table, reject by default.
syslog("err","Request matched no policy rule: ".$request->{command}->[0]);
return 0;
}