Copied tentacle server to other localizations

This commit is contained in:
fermin831 2018-04-27 09:32:53 +02:00
parent 1fa309516f
commit f49e280b13
2 changed files with 290 additions and 80 deletions

View File

@ -102,7 +102,7 @@ my $SERVICE_NAME="Tentacle Server";
my $SERVICE_PARAMS=join(' ', @ARGV);
# Program version
our $VERSION = '0.6.1';
our $VERSION = '0.6.2';
# IPv4 address to listen on
my @t_addresses = ('0', '0.0.0.0');
@ -217,6 +217,7 @@ sub print_help {
print ("\t-d\t\tRun as daemon.\n");
print ("\t-e cert\t\tOpenSSL certificate file. Enables SSL.\n");
print ("\t-f ca_cert\tVerify that the peer certificate is signed by a ca.\n");
print ("\t-F config_file\tConfiguration file full path.\n");
print ("\t-h\t\tShow help.\n");
print ("\t-I\t\tEnable insecure operations (file listing and moving).\n");
print ("\t-i\t\tFilters.\n");
@ -278,11 +279,13 @@ sub daemonize {
################################################################################
sub parse_options {
my %opts;
my $CONF = {};
my $token_value;
my $tmp;
my @t_addresses_tmp;
# Get options
if (getopts ('a:b:c:de:f:g:hIi:k:l:m:op:qr:s:S:t:TvVwx:', \%opts) == 0 || defined ($opts{'h'})) {
if (getopts ('a:b:c:de:f:F:g:hIi:k:l:m:op:qr:s:S:t:TvVwx:', \%opts) == 0 || defined ($opts{'h'})) {
print_help ();
exit 1;
}
@ -304,10 +307,16 @@ sub parse_options {
}
}
# Configuration file
if (defined($opts{'F'})) {
parse_config_file($opts{'F'}, $CONF);
}
# Address
if (defined ($opts{'a'})) {
$token_value = get_config_value($opts{'a'}, $CONF->{'addresses'});
if (defined ($token_value)) {
@t_addresses = ();
@t_addresses_tmp = split(/,/, $opts{'a'});
@t_addresses_tmp = split(/,/, $token_value);
foreach my $t_address (@t_addresses_tmp) {
$t_address =~ s/^ *(.*?) *$/$1/;
@ -323,15 +332,17 @@ sub parse_options {
}
# Maximum simultaneous connections
if (defined ($opts{'c'})) {
$t_max_conn = $opts{'c'};
$token_value = get_config_value($opts{'c'}, $CONF->{'max_connections'});
if (defined ($token_value)) {
$t_max_conn = $token_value;
if ($t_max_conn !~ /^\d+$/ || $t_max_conn < 1) {
error ("Invalid number of maximum simultaneous connections.");
}
}
# Run as daemon
if (defined ($opts{'d'})) {
$token_value = get_config_value($opts{'d'}, $CONF->{'daemon'}, 1);
if (defined ($token_value)) {
if ($^ eq 'MSWin32') {
error ("-d flag not available for this OS.");
}
@ -340,11 +351,12 @@ sub parse_options {
}
# Enable SSL
if (defined ($opts{'e'})) {
$token_value = get_config_value($opts{'e'}, $CONF->{'ssl_cert'});
if (defined ($token_value)) {
require IO::Socket::SSL;
$t_ssl_cert = $opts{'e'};
$t_ssl_cert = $token_value;
if (! -f $t_ssl_cert) {
error ("File $t_ssl_cert does not exist.");
}
@ -353,21 +365,24 @@ sub parse_options {
}
# Verify peer certificate
if (defined ($opts{'f'})) {
$t_ssl_ca = $opts{'f'};
$token_value = get_config_value($opts{'f'}, $CONF->{'ssl_ca'});
if (defined ($token_value)) {
$t_ssl_ca = $token_value;
if (! -f $t_ssl_ca) {
error ("File $t_ssl_ca does not exist.");
}
}
# Insecure mode
if (defined ($opts{'I'})) {
$token_value = get_config_value($opts{'I'}, $CONF->{'insecure'}, 1);
if (defined ($token_value)) {
$t_insecure = 1;
}
# Filters (regexp:dir;regexp:dir...)
if (defined ($opts{'i'})) {
my @filters = split (';', $opts{'i'});
$token_value = get_config_value($opts{'i'}, $CONF->{'filters'});
if (defined ($token_value)) {
my @filters = split (';', $token_value);
foreach my $filter (@filters) {
my ($regexp, $dir) = split (':', $filter);
next unless defined ($regexp) && defined ($dir);
@ -381,51 +396,58 @@ sub parse_options {
}
# SSL private key file
if (defined ($opts{'k'})) {
$t_ssl_key = $opts{'k'};
$token_value = get_config_value($opts{'k'}, $CONF->{'ssl_key'});
if (defined ($token_value)) {
$t_ssl_key = $token_value;
if (! -f $t_ssl_key) {
error ("File $t_ssl_key does not exist.");
}
}
# Maximum file size
if (defined ($opts{'m'})) {
$t_max_size = $opts{'m'};
$token_value = get_config_value($opts{'m'}, $CONF->{'max_size'});
if (defined ($token_value)) {
$t_max_size = $token_value;
if ($t_max_size !~ /^\d+$/ || $t_max_size < 1) {
error ("Invalid maximum file size.");
}
}
# File overwrite
if (defined ($opts{'o'})) {
$token_value = get_config_value($opts{'o'}, $CONF->{'overwrite'}, 1);
if (defined ($token_value)) {
$t_overwrite = 1;
}
# Port
if (defined ($opts{'p'})) {
$t_port = $opts{'p'};
$token_value = get_config_value($opts{'p'}, $CONF->{'port'});
if (defined ($token_value)) {
$t_port = $token_value;
if ($t_port !~ /^\d+$/ || $t_port < 1 || $t_port > 65535) {
error ("Port $t_port is not valid.");
}
}
# Quiet mode
if (defined ($opts{'q'})) {
$token_value = get_config_value($opts{'q'}, $CONF->{'quiet'}, 1);
if (defined ($token_value)) {
$t_quiet = 1;
}
# Retries
if (defined ($opts{'r'})) {
$t_retries = $opts{'r'};
$token_value = get_config_value($opts{'r'}, $CONF->{'retries'});
if (defined ($token_value)) {
$t_retries = $token_value;
if ($t_retries !~ /^\d+$/ || $t_retries < 1) {
error ("Invalid number of retries for network operations.");
}
}
# Storage directory
if (defined ($opts{'s'})) {
$token_value = get_config_value($opts{'s'}, $CONF->{'directory'});
if (defined ($token_value)) {
$t_directory = $opts{'s'};
$t_directory = $token_value;
# Check that directory exists
if (! -d $t_directory) {
@ -444,25 +466,36 @@ sub parse_options {
}
}
else {
if (! defined($opts{'b'})) {
$token_value = get_config_value($opts{'b'}, $CONF->{'proxy_ip'});
if (! defined($token_value)) {
print_help ();
exit 1;
}
}
# Timeout
if (defined ($opts{'t'})) {
$t_timeout = $opts{'t'};
$token_value = get_config_value($opts{'t'}, $CONF->{'timeout'});
if (defined ($token_value)) {
$t_timeout = $token_value;
if ($t_timeout !~ /^\d+$/ || $t_timeout < 1) {
error ("Invalid timeout for network operations.");
}
}
# Read verbose from config file
if (defined($CONF->{'verbose'})) {
if ($CONF->{'verbose'} eq "1") {
$t_log = 1;
} elsif ($CONF->{'verbose'} eq "2") {
$t_log = 1;
$t_log_hard = 1;
}
}
# Be verbose
if (defined ($opts{'v'})) {
$t_log = 1;
$t_log_hard = 0;
}
# Be verbose hard
if (defined ($opts{'V'})) {
$t_log = 1;
@ -470,18 +503,21 @@ sub parse_options {
}
# SSL private key password
if (defined ($opts{'w'})) {
$token_value = get_config_value($opts{'w'}, $CONF->{'ssl_password'}, 1);
if (defined ($token_value)) {
$t_ssl_pwd = ask_passwd ("Enter private key file password: ", "Enter private key file password again for confirmation: ");
}
# Server password
if (defined ($opts{'x'})) {
$t_pwd = $opts{'x'};
$token_value = get_config_value($opts{'x'}, $CONF->{'password'});
if (defined ($token_value)) {
$t_pwd = $token_value;
}
#Proxy IP address
if (defined ($opts{'b'})) {
$t_proxy_ip = $opts{'b'};
$token_value = get_config_value($opts{'b'}, $CONF->{'proxy_ip'});
if (defined ($token_value)) {
$t_proxy_ip = $token_value;
if ($t_proxy_ip !~ /^[a-zA-Z\.]+$/ && ($t_proxy_ip !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
|| $1 < 0 || $1 > 255 || $2 < 0 || $2 > 255
|| $3 < 0 || $3 > 255 || $4 < 0 || $4 > 255) &&
@ -491,15 +527,17 @@ sub parse_options {
}
# Proxy Port
if (defined ($opts{'g'})) {
$t_proxy_port = $opts{'g'};
$token_value = get_config_value($opts{'g'}, $CONF->{'proxy_port'});
if (defined ($token_value)) {
$t_proxy_port = $token_value;
if ($t_proxy_port !~ /^\d+$/ || $t_proxy_port < 1 || $t_proxy_port > 65535) {
error ("Proxy port $t_port is not valid.");
}
}
# TCP wrappers support
if (defined ($opts{'T'})) {
$token_value = get_config_value($opts{'T'}, $CONF->{'use_libwrap'}, 1);
if (defined ($token_value)) {
if ($t_libwrap_installed) {
$t_use_libwrap = 1;
} else {
@ -531,9 +569,76 @@ sub parse_options {
}
# Get the config file
if (defined ($opts{'l'})) {
$log_file = $opts{'l'};
$token_value = get_config_value($opts{'l'}, $CONF->{'log_file'});
if (defined ($token_value)) {
$log_file = $token_value;
}
# No command lines config values
# Get the block size
if (defined ($CONF->{'block_size'})) {
if ($t_port !~ /^\d+$/ || $t_port < 1) {
error ("Invalid block size: " . $CONF->{'block_size'} . ".");
}
$t_block_size = $CONF->{'block_size'};
}
# Configuration file invalid chars
if (defined ($CONF->{'invalid_chars'})) {
$t_invalid_chars = $CONF->{'invalid_chars'};
}
}
################################################################################
## SUB parse_config_file
## Get all options from a config file.
################################################################################
sub parse_config_file {
my ($config_file, $CONF) = @_;
# File should be writable
if (! -r $config_file) {
print "Configuration file $config_file is not readable.\n";
return;
}
# Open the file
my $FH;
if (! open ($FH, "< $config_file")) {
print "Cannot open configuration file $config_file.\n";
return;
}
# Read the file and only get the well formed lines
while (<$FH>) {
my $buffer_line = $_;
if ($buffer_line =~ /^[a-zA-Z]/){ # begins with letters
if ($buffer_line =~ m/([\w\-\_\.]+)\s+(.*)/){
$CONF->{$1} = $2 unless $2 eq "";
}
}
}
close ($FH);
return;
}
################################################################################
## SUB parse_config_file
## Search in command line options and config hash from configuration file
## to get a value (command line is a priority)
################################################################################
sub get_config_value {
my ($cmd_value, $conf_value, $bool) = @_;
$bool = 0 unless defined($bool);
return $cmd_value if defined($cmd_value);
# The boolean type value is 1 or undef (0 should be translated like undefP)
if ($bool && defined($conf_value)) {
return undef if ($conf_value ne "1");
}
return $conf_value;
}
################################################################################

View File

@ -102,7 +102,7 @@ my $SERVICE_NAME="Tentacle Server";
my $SERVICE_PARAMS=join(' ', @ARGV);
# Program version
our $VERSION = '0.6.1';
our $VERSION = '0.6.2';
# IPv4 address to listen on
my @t_addresses = ('0', '0.0.0.0');
@ -217,6 +217,7 @@ sub print_help {
print ("\t-d\t\tRun as daemon.\n");
print ("\t-e cert\t\tOpenSSL certificate file. Enables SSL.\n");
print ("\t-f ca_cert\tVerify that the peer certificate is signed by a ca.\n");
print ("\t-F config_file\tConfiguration file full path.\n");
print ("\t-h\t\tShow help.\n");
print ("\t-I\t\tEnable insecure operations (file listing and moving).\n");
print ("\t-i\t\tFilters.\n");
@ -278,11 +279,13 @@ sub daemonize {
################################################################################
sub parse_options {
my %opts;
my $CONF = {};
my $token_value;
my $tmp;
my @t_addresses_tmp;
# Get options
if (getopts ('a:b:c:de:f:g:hIi:k:l:m:op:qr:s:S:t:TvVwx:', \%opts) == 0 || defined ($opts{'h'})) {
if (getopts ('a:b:c:de:f:F:g:hIi:k:l:m:op:qr:s:S:t:TvVwx:', \%opts) == 0 || defined ($opts{'h'})) {
print_help ();
exit 1;
}
@ -304,10 +307,16 @@ sub parse_options {
}
}
# Configuration file
if (defined($opts{'F'})) {
parse_config_file($opts{'F'}, $CONF);
}
# Address
if (defined ($opts{'a'})) {
$token_value = get_config_value($opts{'a'}, $CONF->{'addresses'});
if (defined ($token_value)) {
@t_addresses = ();
@t_addresses_tmp = split(/,/, $opts{'a'});
@t_addresses_tmp = split(/,/, $token_value);
foreach my $t_address (@t_addresses_tmp) {
$t_address =~ s/^ *(.*?) *$/$1/;
@ -323,15 +332,17 @@ sub parse_options {
}
# Maximum simultaneous connections
if (defined ($opts{'c'})) {
$t_max_conn = $opts{'c'};
$token_value = get_config_value($opts{'c'}, $CONF->{'max_connections'});
if (defined ($token_value)) {
$t_max_conn = $token_value;
if ($t_max_conn !~ /^\d+$/ || $t_max_conn < 1) {
error ("Invalid number of maximum simultaneous connections.");
}
}
# Run as daemon
if (defined ($opts{'d'})) {
$token_value = get_config_value($opts{'d'}, $CONF->{'daemon'}, 1);
if (defined ($token_value)) {
if ($^ eq 'MSWin32') {
error ("-d flag not available for this OS.");
}
@ -340,11 +351,12 @@ sub parse_options {
}
# Enable SSL
if (defined ($opts{'e'})) {
$token_value = get_config_value($opts{'e'}, $CONF->{'ssl_cert'});
if (defined ($token_value)) {
require IO::Socket::SSL;
$t_ssl_cert = $opts{'e'};
$t_ssl_cert = $token_value;
if (! -f $t_ssl_cert) {
error ("File $t_ssl_cert does not exist.");
}
@ -353,21 +365,24 @@ sub parse_options {
}
# Verify peer certificate
if (defined ($opts{'f'})) {
$t_ssl_ca = $opts{'f'};
$token_value = get_config_value($opts{'f'}, $CONF->{'ssl_ca'});
if (defined ($token_value)) {
$t_ssl_ca = $token_value;
if (! -f $t_ssl_ca) {
error ("File $t_ssl_ca does not exist.");
}
}
# Insecure mode
if (defined ($opts{'I'})) {
$token_value = get_config_value($opts{'I'}, $CONF->{'insecure'}, 1);
if (defined ($token_value)) {
$t_insecure = 1;
}
# Filters (regexp:dir;regexp:dir...)
if (defined ($opts{'i'})) {
my @filters = split (';', $opts{'i'});
$token_value = get_config_value($opts{'i'}, $CONF->{'filters'});
if (defined ($token_value)) {
my @filters = split (';', $token_value);
foreach my $filter (@filters) {
my ($regexp, $dir) = split (':', $filter);
next unless defined ($regexp) && defined ($dir);
@ -381,51 +396,58 @@ sub parse_options {
}
# SSL private key file
if (defined ($opts{'k'})) {
$t_ssl_key = $opts{'k'};
$token_value = get_config_value($opts{'k'}, $CONF->{'ssl_key'});
if (defined ($token_value)) {
$t_ssl_key = $token_value;
if (! -f $t_ssl_key) {
error ("File $t_ssl_key does not exist.");
}
}
# Maximum file size
if (defined ($opts{'m'})) {
$t_max_size = $opts{'m'};
$token_value = get_config_value($opts{'m'}, $CONF->{'max_size'});
if (defined ($token_value)) {
$t_max_size = $token_value;
if ($t_max_size !~ /^\d+$/ || $t_max_size < 1) {
error ("Invalid maximum file size.");
}
}
# File overwrite
if (defined ($opts{'o'})) {
$token_value = get_config_value($opts{'o'}, $CONF->{'overwrite'}, 1);
if (defined ($token_value)) {
$t_overwrite = 1;
}
# Port
if (defined ($opts{'p'})) {
$t_port = $opts{'p'};
$token_value = get_config_value($opts{'p'}, $CONF->{'port'});
if (defined ($token_value)) {
$t_port = $token_value;
if ($t_port !~ /^\d+$/ || $t_port < 1 || $t_port > 65535) {
error ("Port $t_port is not valid.");
}
}
# Quiet mode
if (defined ($opts{'q'})) {
$token_value = get_config_value($opts{'q'}, $CONF->{'quiet'}, 1);
if (defined ($token_value)) {
$t_quiet = 1;
}
# Retries
if (defined ($opts{'r'})) {
$t_retries = $opts{'r'};
$token_value = get_config_value($opts{'r'}, $CONF->{'retries'});
if (defined ($token_value)) {
$t_retries = $token_value;
if ($t_retries !~ /^\d+$/ || $t_retries < 1) {
error ("Invalid number of retries for network operations.");
}
}
# Storage directory
if (defined ($opts{'s'})) {
$token_value = get_config_value($opts{'s'}, $CONF->{'directory'});
if (defined ($token_value)) {
$t_directory = $opts{'s'};
$t_directory = $token_value;
# Check that directory exists
if (! -d $t_directory) {
@ -444,25 +466,36 @@ sub parse_options {
}
}
else {
if (! defined($opts{'b'})) {
$token_value = get_config_value($opts{'b'}, $CONF->{'proxy_ip'});
if (! defined($token_value)) {
print_help ();
exit 1;
}
}
# Timeout
if (defined ($opts{'t'})) {
$t_timeout = $opts{'t'};
$token_value = get_config_value($opts{'t'}, $CONF->{'timeout'});
if (defined ($token_value)) {
$t_timeout = $token_value;
if ($t_timeout !~ /^\d+$/ || $t_timeout < 1) {
error ("Invalid timeout for network operations.");
}
}
# Read verbose from config file
if (defined($CONF->{'verbose'})) {
if ($CONF->{'verbose'} eq "1") {
$t_log = 1;
} elsif ($CONF->{'verbose'} eq "2") {
$t_log = 1;
$t_log_hard = 1;
}
}
# Be verbose
if (defined ($opts{'v'})) {
$t_log = 1;
$t_log_hard = 0;
}
# Be verbose hard
if (defined ($opts{'V'})) {
$t_log = 1;
@ -470,18 +503,21 @@ sub parse_options {
}
# SSL private key password
if (defined ($opts{'w'})) {
$token_value = get_config_value($opts{'w'}, $CONF->{'ssl_password'}, 1);
if (defined ($token_value)) {
$t_ssl_pwd = ask_passwd ("Enter private key file password: ", "Enter private key file password again for confirmation: ");
}
# Server password
if (defined ($opts{'x'})) {
$t_pwd = $opts{'x'};
$token_value = get_config_value($opts{'x'}, $CONF->{'password'});
if (defined ($token_value)) {
$t_pwd = $token_value;
}
#Proxy IP address
if (defined ($opts{'b'})) {
$t_proxy_ip = $opts{'b'};
$token_value = get_config_value($opts{'b'}, $CONF->{'proxy_ip'});
if (defined ($token_value)) {
$t_proxy_ip = $token_value;
if ($t_proxy_ip !~ /^[a-zA-Z\.]+$/ && ($t_proxy_ip !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
|| $1 < 0 || $1 > 255 || $2 < 0 || $2 > 255
|| $3 < 0 || $3 > 255 || $4 < 0 || $4 > 255) &&
@ -491,15 +527,17 @@ sub parse_options {
}
# Proxy Port
if (defined ($opts{'g'})) {
$t_proxy_port = $opts{'g'};
$token_value = get_config_value($opts{'g'}, $CONF->{'proxy_port'});
if (defined ($token_value)) {
$t_proxy_port = $token_value;
if ($t_proxy_port !~ /^\d+$/ || $t_proxy_port < 1 || $t_proxy_port > 65535) {
error ("Proxy port $t_port is not valid.");
}
}
# TCP wrappers support
if (defined ($opts{'T'})) {
$token_value = get_config_value($opts{'T'}, $CONF->{'use_libwrap'}, 1);
if (defined ($token_value)) {
if ($t_libwrap_installed) {
$t_use_libwrap = 1;
} else {
@ -531,9 +569,76 @@ sub parse_options {
}
# Get the config file
if (defined ($opts{'l'})) {
$log_file = $opts{'l'};
$token_value = get_config_value($opts{'l'}, $CONF->{'log_file'});
if (defined ($token_value)) {
$log_file = $token_value;
}
# No command lines config values
# Get the block size
if (defined ($CONF->{'block_size'})) {
if ($t_port !~ /^\d+$/ || $t_port < 1) {
error ("Invalid block size: " . $CONF->{'block_size'} . ".");
}
$t_block_size = $CONF->{'block_size'};
}
# Configuration file invalid chars
if (defined ($CONF->{'invalid_chars'})) {
$t_invalid_chars = $CONF->{'invalid_chars'};
}
}
################################################################################
## SUB parse_config_file
## Get all options from a config file.
################################################################################
sub parse_config_file {
my ($config_file, $CONF) = @_;
# File should be writable
if (! -r $config_file) {
print "Configuration file $config_file is not readable.\n";
return;
}
# Open the file
my $FH;
if (! open ($FH, "< $config_file")) {
print "Cannot open configuration file $config_file.\n";
return;
}
# Read the file and only get the well formed lines
while (<$FH>) {
my $buffer_line = $_;
if ($buffer_line =~ /^[a-zA-Z]/){ # begins with letters
if ($buffer_line =~ m/([\w\-\_\.]+)\s+(.*)/){
$CONF->{$1} = $2 unless $2 eq "";
}
}
}
close ($FH);
return;
}
################################################################################
## SUB parse_config_file
## Search in command line options and config hash from configuration file
## to get a value (command line is a priority)
################################################################################
sub get_config_value {
my ($cmd_value, $conf_value, $bool) = @_;
$bool = 0 unless defined($bool);
return $cmd_value if defined($cmd_value);
# The boolean type value is 1 or undef (0 should be translated like undefP)
if ($bool && defined($conf_value)) {
return undef if ($conf_value ne "1");
}
return $conf_value;
}
################################################################################