#!/usr/bin/perl -w -T
# +=======================================================================+
# || /usr/sbin/cipux_rpcdr                                               ||
# ||                                                                     ||
# || Start the CipUX RPC remote daemon                                   ||
# ||                                                                     ||
# || (C) Copyright 2007 by Christian Kuelker                             ||
# || (C) Copyright 2007 by Sebastian Friedel                             ||
# || (C) Copyright 2008 - 2009 by Christian Kuelker                      ||
# || All rights reserved!                                                ||
# ||                                                                     ||
# || License: GNU General Public License - GNU GPL - version 2           ||
# ||          or (at your opinion) any later version.                    ||
# ||                                                                     ||
# +=======================================================================+
# ID:       $Id$
# Revision: $Revision$
# Head URL: $HeadURL$
# Date:     $Date$
# Source:   $Source$

package cipux_rpcdr;

use strict;
use warnings;
use Carp;
use CipUX;
use Fatal qw(open close);
use English qw( -no_match_vars);
use Readonly;
use version; our $VERSION = qv('3.4.0.0');

# +=========================================================================+
# || CONSTANTS                                                             ||
# +=========================================================================+
delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)};    # Make %ENV safer
Readonly::Scalar my $EMPTY_STRING => q{};
Readonly::Scalar my $SCRIPT       => 'cipux_rpcdr';
local $PROGRAM_NAME = "$SCRIPT [accepting connections]";

# +=========================================================================+
# || GLOBAL VARS                                                           ||
# +=========================================================================+
my $stunnel = $EMPTY_STRING;
my $pid     = 0;

# we need to know the location of stunnel, because we use this path
# as input for pidof. so we can not use PATH for pidof.

if ( -x '/usr/bin/stunnel4' ) {
    $stunnel = '/usr/bin/stunnel4';
}
elsif ( -x '/usr/sbin/stunnel4' ) {
    $stunnel = '/usr/sbin/stunnel4';
}
else {
    croak "stunnel4 was not found in /usr/bin or /usr/sbin!\n";
}

open my $stdin, q{|-}, "/bin/pidof -s $stunnel "
    or croak "Can not open $stunnel\n";
my @pid = <$stdin>;
close $stdin or croak 'Can not close access to pidof!';

# untaint data
my $cipux = CipUX->new( { debug => 0 } );
foreach my $p (@pid) {
    $cipux->l($p);
}

if ( defined $pid[0] ) {
    $pid = $pid[0];
}

chomp $pid;
if ( $pid == 0 ) {
    system "$stunnel /etc/cipux/stunnel.conf";
}
else {
    die "stunnel (alias cipux_rpcdr) is already running at $pid!\n";
}

exit 0;

__END__

#===========================================================================
#==== Documentation ========================================================
#===========================================================================

=pod

=head1 NAME

cipux_rpcdr - Remote CipUX XML-RPC Sever wrapper for Stunnel

=head1 VERSION

version 3.4.0.0

=head1 SYNOPSIS

        cipux_rpcdr

=head1 DESCRIPTION

This is the CipUX XML-RPC remote daemon for the CipUX::Task API. The daemon
provides CipUX commands to RPC clients over SSL. Start the cipux_rpcdr server
after the cipux_rpcd server.

=head1 USAGE

 /usr/sbin/cipux_rpcdr

=head1 REQUIRED ARGUMENTS

None.

=head1 OPTIONS

TODO

=head1 DIAGNOSTICS

TODO

=head1 CONFIGURATION

Need stunnel configuration.

TODO

=head1 DEPENDENCIES

 Carp
 CipUX
 English
 Fatal
 Readonly
 version

=head1 INCOMPATIBILITIES

Not known.

=head1 BUGS AND LIMITATIONS

Not known.

=head1 SEE ALSO

See the CipUX webpage and the manual at L<http://www.cipux.org>
See the mailing list L<http://sympa.cipworx.org/wws/info/cipux-devel>

=head1 AUTHOR

Christian Kuelker  E<lt>christian.kuelker@cipworx.orgE<gt>

=head1 LICENSE AND COPYRIGHT

Copyright (C) 2007 - 2009 by Christian Kuelker

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, 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

=cut

