This repository has been archived by the owner on Jul 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
satan.pl
executable file
·248 lines (225 loc) · 8.5 KB
/
satan.pl
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/perl
## Satan
# Shell account service manager
# Rootnode http://rootnode.net
#
# Copyright (C) 2009-2011 Marcin Hlybin
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
use IO::Socket;
use DBI;
use Data::Dumper;
use feature 'switch';
use warnings;
use strict;
use utf8;
use Satan::FTP;
use Satan::Backup;
use Satan::Account;
use Satan::VPN;
$|++;
$SIG{CHLD} = 'IGNORE'; # don't wait for retarded kids
unless (@ARGV) {
open STDOUT,">>","/adm/satan/access.log";
open STDERR,">>","/adm/satan/error.log";
chmod 0600,"/adm/satan/access.log";
chmod 0600,"/adm/satan/error.log";
}
my $sockfile = '/adm/satan/satan.sock';
`rm -f -- /adm/satan/lock/*`;
unlink $sockfile;
my $socket = new IO::Socket::UNIX (
Local => $sockfile,
Type => SOCK_STREAM,
Listen => 10,
Reuse => 1,
);
die "Could not create UNIX socket: $!\n" unless $socket;
chmod 0666, $sockfile;
binmode( $socket, ':utf8' );
## dbi
my $dbh_system = DBI->connect("dbi:mysql:rootnode;mysql_read_default_file=/root/.my.system.cnf",undef,undef,{ RaiseError => 0, AutoCommit => 1 });
$dbh_system->{mysql_auto_reconnect} = 1;
$dbh_system->{mysql_enable_utf8} = 1;
## main
while(my $client = $socket->accept()) {
binmode( $client, ':utf8' );
setsockopt $client, SOL_SOCKET, SO_PASSCRED, 1;
my($pid,$uid,$gid) = unpack "iii", $client->sockopt(SO_PEERCRED);
my $now = localtime(time);
if(not defined $uid) {
print "[$now] WARNING: UID not defined!\n";
close($client);
next;
}
if($uid < 2000 or $uid >= 65000) {
print "[$now] WARNING: UID $uid is not allowed to use Satan!\n";
close($client);
next;
}
if(fork() == 0) {
if(-f "/adm/satan/lock/$uid" and $uid) {
print "[$now] Connection refused: too many sessions for user $uid.\n";
print $client "Only one session is allowed. Dying.\n";
close($client);
exit 0;
} else {
open LOCK,">","/adm/satan/lock/$uid";
close LOCK;
}
$client->autoflush(1);
my $login = getpwuid($uid);
if(not defined $login) {
print "[$now] ERROR: No login name for user $uid!\n";
print $client "Where is your login name?\n";
close($client);
next;
}
while(<$client>) {
chomp;
my @args = split(/\s/,$_);
my $pwd = shift @args;
my $daemon = shift @args || 'help';
my @params = ($uid,$login,$client,@args);
my $now = localtime(time);
print "[$now] Connection: $login ($uid), Request: $daemon(".join(' ',@args).")\n";
my $return;
given($daemon) {
## Satan::FTP
when ("ftp") {
my $ftp = Satan::FTP->new(
login => $login,
uid => $uid,
client => $client,
dbh_system => $dbh_system
);
## commands
my $command = shift @args || 'list';
$command = 'help' if grep(/^(help|\?)$/,@args);
given($command) {
when ('add') { $return = $ftp->add(@args) }
when ('del') { $return = $ftp->del(@args) }
when ('list') { $return = $ftp->list(@args) }
when (/^(change|modify)$/) { $return = $ftp->modify(@args) }
when ('help') { $return = $ftp->help(@args) }
default {
print $client "Command '$command' is not available. Available commands are: add, del, change, list.\n";
print $client "See 'satan ftp help' or http://rootnode.net/satan/ftp for details.\n";
}
}
}
## Satan::Account
when ('account') {
my $account = Satan::Account->new(
login => $login,
uid => $uid,
client => $client,
dbh_system => $dbh_system,
);
## commands
my $command = shift @args || 'show';
$command = 'help' if grep(/^(help|\?)$/,@args);
given($command) {
when('show') { $return = $account->show(@args) }
when('edit') { $return = $account->edit(@args) }
when('pay') { $return = $account->pay(@args) }
when('help') { $return = $account->help(@args) }
default {
print $client "Command '$command' is not available. Available commands are: show, edit, pay.\n";
print $client "See 'satan account help' or http://rootnode.net/satan/account for details.\n";
}
}
}
## Satan::Backup
when ('backup') {
my $backup = Satan::Backup->new(
login => $login,
uid => $uid,
pwd => $pwd,
dbh_system => $dbh_system
);
## commands
my $command = shift @args || 'list';
$command = 'help' if grep(/^(help|\?)$/,@args);
given($command) {
when ('add') { $return = $backup->add(@args) }
when ('del') { $return = $backup->del(@args) }
when (/^(include|exclude)$/) { $return = $backup->include($1,@args) }
when ('list') { $return = $backup->list(@args) }
when ('help') { $return = $backup->help(@args) }
default {
print $client "Command '$command' is not available. Available commands are: add, del, include, exclude, list.\n";
print $client "See 'satan backup help' or http://rootnode.net/satan/backup for details.\n";
}
}
}
## Satan::VPN
when ('vpn') {
my $vpn = Satan::VPN->new(
login => $login,
uid => $uid,
client => $client,
dbh_system => $dbh_system
);
## commands
my $command = shift @args || 'list';
$command = 'help' if grep(/^(help|\?)$/,@args);
given($command) {
when('add') { $return = $vpn->add(@args) }
when('del') { $return = $vpn->del(@args) }
when('list') { $return = $vpn->list(@args) }
when('config') { $return = $vpn->config(@args) }
when('help') { $return = $vpn->help(@args) }
default {
print $client "Command '$command' is not available. Available command are: add, del, list, config.\n";
print $client "See 'satan vpn help' or http://rootnode.net/satan/vpn for details.\n";
}
}
}
## Help
when ("help") {
my $usage = "\033[1mSatan, the most hellish service manager\033[0m\n"
. "Usage: satan [SERVICE] [TASK] [ARGS]\n\n"
. "Available services: \033[1;32mmysql pgsql ftp domain vhost dns mail backup account vpn\033[0m\n"
. "Type help at the end of each command to see detailed description, e.g.:\n"
. "\033[1;34m\$ satan mysql help\033[0m\n\n"
. "For additional information, see http://rootnode.net\n"
. "Bug reporting on mailing list.\n\n";
#. "Bash completion is supported. Press TAB twice.\n";
print $client $usage;
}
default {
print $client "Available satan serices: ftp backup\n";
}
}
print $client $return."\n" if $return;
last;
}
close($client);
unlink "/adm/satan/lock/$uid";
exit;
}
}
close($socket);
close STDOUT;
close STDERR;