-
Notifications
You must be signed in to change notification settings - Fork 0
/
perd
executable file
·127 lines (106 loc) · 2.65 KB
/
perd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/perl
#
# Author: Michal Grezl <[email protected]>
#
#
### BEGIN INIT INFO
# Provides: perd
# Required-Start: $remote_fs
# Should-Start: $syslog $named
# Required-Stop: $remote_fs
# Should-Stop: $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: POP, IMAP and managesieve proxy
# Description: Starts perdition to allow proxied POP, IMAP and managesieve access
### END INIT INFO
use File::Slurp;
my $action = $ARGV[0];
if ($action eq "") {
print "hi, need params\n";
exit 1;
}
#my $config_dir = "/etc/perdition";
my $config_dir = "/etc/perdition/config";
opendir(my $dh, $config_dir) || die "Can't open $config_dir: $!";
while (readdir $dh) {
if (/\.[0123456789]\.conf$/) {
my $fh;
print "Reading $config_dir/$_ ...\n";
open($fh, "<$config_dir/$_");
my @fc = <$fh>;
my @x = grep(/protocol/, @fc);
my $proto = (split(" ", $x[0]))[1];
&do_stuff($action, "$config_dir/$_", $proto)
} else {
print "not doing:$_\n";
}
}
closedir $dh;
sub do_stuff()
{
($action, $file, $proto) = @_;
if ($action eq "start") {
&start($file, $proto);
} elsif ($action eq "stop") {
&stop($file, $proto);
} elsif ($action eq "list") {
&list($file, $proto);
} elsif ($action eq "status") {
} elsif ($action eq "about") {
print "Perdition start script\n";
}
}
sub list()
{
($file, $proto) = @_;
my $bind_addr;
open(my $fh, "<$file");
my @fc = <$fh>;
foreach $i (grep($_ =~ "bind_address", @fc)) {
$bind_addr = $i;
if($i =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) {
$bind_addr = $1;
}
}
print " proto $proto ...\n";
print " ip $bind_addr ...\n";
close($fh);
}
sub start()
{
($file, $proto) = @_;
print "Starting $file as $proto ...";
if ($proto eq "imap4s") {
$DAEMON = "/usr/sbin/perdition.imaps";
system($DAEMON, "-f", "$file");
} elsif ($proto eq "imap4") {
$DAEMON = "/usr/sbin/perdition.imap4";
system($DAEMON, "-f", "$file");
} else {
print "tbd\n";
}
if ($? == -1) {
print "failed to execute: $!\n";
} elsif ($? & 127) {
printf "child died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
} else {
printf "[done] %d\n", $? >> 8;
}
}
sub stop
{
($file, $proto) = @_;
open($fh, "<$file");
my @fc = <$fh>;
my @x = grep(/pid_file/, @fc);
my $pid_file = (split(" ", $x[0]))[1];
my $pid = read_file($pid_file, err_mode => "quiet") or do {
print "Pid file does not exist $pid_file ...\n";
return;
};
print "Stoping $file ($pid) ...";
system ("/bin/kill", $pid);
printf "[done] %d\n", $? >> 8;
}