Skip to content

Instantly share code, notes, and snippets.

@ggl
Last active February 21, 2021 13:34
Show Gist options
  • Save ggl/29bd58c0ad57bb40a48a2749b3acb89d to your computer and use it in GitHub Desktop.
Save ggl/29bd58c0ad57bb40a48a2749b3acb89d to your computer and use it in GitHub Desktop.
Pure-FTPd configuration generator
#!/usr/bin/env perl
#
# Generates pureftpd.config options in the conf/ directory for use
# with pure-ftpd-wrapper
#
use strict;
use warnings;
use Getopt::Long;
# Process command line parameters
my %opts = (
'conf' => '/etc/pure-ftpd/pure-ftpd.conf',
'dir' => '/etc/pure-ftpd/conf',
);
Getopt::Long::GetOptions(
'conf=s' => \$opts{'conf'},
'dir=s' => \$opts{'dir'},
);
my %conf;
if (-d $opts{'dir'} and -s $opts{'conf'}) {
open(my $fh, '<:encoding(UTF-8)', $opts{'conf'})
or die "Cannot open file $opts{conf}: $!\n";
while (my $line = <$fh>) {
if ($line =~ /^(\w+)\s+(\d+(\s+|:)\d+|\w+)/ and $1 and $2) {
$conf{$1} = $2;
$conf{$1} =~ s/:/ /g;
};
};
close($fh);
foreach my $key (sort keys %conf) {
my $opt_fn = $opts{'dir'}.'/'.$key;
if (-f $opt_fn or !-e $opt_fn) {
open(my $fh, '>:encoding(UTF-8)', $opt_fn)
or die "Cannot open file $opt_fn: $!\n";
print $fh "$conf{$key}\n";
close $fh;
}
else {
die "Refusing to write to special file: $opt_fn\n";
};
};
}
elsif (! -s $opts{'conf'}) {
die "Unable to find config file $opts{'conf'}\n";
}
elsif (! -d $opts{'dir'}) {
die "Unable to find config directory $opts{'dir'}\n";
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment