forked from KrzysztofKowalski/satan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.pl
284 lines (236 loc) · 6.57 KB
/
exec.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/perl -l
#
# Satan (exec)
# Shell account service manager
# Rootnode http://rootnode.net
#
# Copyright (C) 2009-2012 Marcin Hlybin
# All rights reserved.
#
use warnings;
use strict;
use JSON::XS;
use English;
use File::Path qw(make_path remove_tree);
use File::Copy;
use File::Slurp;
use IO::Socket;
use Digest::MD5 qw(md5_base64);
use Readonly;
no Smart::Comments;
$|++;
$SIG{CHLD} = 'IGNORE'; # braaaaains!!!
Readonly my $EXEC_HOST => '127.0.0.1';
Readonly my $EXEC_PORT => '999';
Readonly my $EXEC_KEY => '/home/etc/satan/key';
Readonly my $TEMPLATE_DIR => '/usr/satan/templates';
# json serialization
my $json = JSON::XS->new->utf8;
# create socket
my $s_exec = IO::Socket::INET->new(
LocalAddr => $EXEC_HOST,
LocalPort => $EXEC_PORT,
Proto => 'tcp',
Listen => 1,
ReuseAddr => 1,
) or die "Cannot create socket! $!\n";
# get exec key
-f $EXEC_KEY or die "Cannot find key file ($EXEC_KEY).\n";
open my $fh, '<', $EXEC_KEY or die "Cannot open key file ($EXEC_KEY).\n";
my (undef, $exec_key) = split /\s+/, <$fh>;
chomp $exec_key;
$exec_key = md5_base64($exec_key);
while (my $s_server = $s_exec->accept()) {
$s_server->autoflush(1);
# spawn child with user privileges
if (fork() == 0) {
my ($is_auth, $response);
SERVER:
while (<$s_server>) {
chomp; /^$/ and next SERVER;
# decode request
my @request = @{ $json->decode($_) };
### Request: @request
my ($auth_key, $uid, $subsystem, $command, @args) = @request;
# check if authorized
if ($auth_key ne $exec_key) {
$response = { status => 401, message => 'Executor: Unauthorized' };
last SERVER;
}
# change user
my $user_name = getpwuid($uid);
if (defined $user_name) {
$EGID = $uid;
$EUID = $uid;
}
else {
$response = { status => 503, message => "Executor: no such user ($uid) in container" };
last SERVER;
}
# run commands
my $action = join('_', $subsystem, $command);
eval {
$response = __PACKAGE__->$action({
args => \@args,
uid => $uid,
user_name => $user_name,
});
}
or do {
$response = { status => 503, message => "Executor: cannot run action $action in container: $@" };
last SERVER;
};
last SERVER;
}
print $s_server $json->encode($response);
close($s_server);
exit;
}
}
sub template {
my ($template_name, $value_of) = @_;
my $template;
### Variables: $value_of
# read template file
my $template_file = "$TEMPLATE_DIR/$template_name.tmpl";
### $template_file
if (-f $template_file) {
open my $fh, '<', $template_file or die "Cannot open template file $template_file";
local $/;
$template = <$fh>;
close $fh;
}
else {
die "Template $template_name file ($template_file) not found.";
}
# check if empty template
if (!$template) {
die "Template is empty.";
}
# convert template
foreach my $var (keys %$value_of) {
$template =~ s/%%\Q$var\E%%/$value_of->{$var}/gi;
delete $value_of->{$var};
}
# check if all variables used
if (%$value_of) {
die "Not enough variables in template. Surplus vars: " . keys %$value_of;
}
# check if no variables left in template
if (my @variables_left = $template =~ /%%(.+)%%/) {
die "Not all variables from template are changed: ". join(', ', @variables_left);
}
### Template: $template;
return $template;
}
sub vhost_del {
my ($self, $client) = @_;
my ($vhost_name) = @{ $client->{args} };
my $vhost_path = "/home/$client->{user_name}/web/$vhost_name";
# move vhost directory
if (-d $vhost_path) {
move($vhost_path, $vhost_path.' (deleted)')
or return {
status => 403,
message => "Executor: cannot delete directory $vhost_path. Probably permission problem"
};
} else {
return {
status => 404,
message => "Executor: vhost directory $vhost_path NOT found. Cannot delete"
};
}
# apache2 config
my $apache2_config_file = "/etc/apache2/sites-enabled/$vhost_name";
if (-l $apache2_config_file) {
unlink $apache2_config_file;
}
# nginx config
my $nginx_config_file = "/etc/nginx/sites-enabled/$vhost_name";
if (-l $nginx_config_file) {
unlink $nginx_config_file;
}
return { status => 0, message => 'OK' };
}
sub vhost_add {
my ($self, $client) = @_;
my ($vhost_name) = @{ $client->{args} };
my $web_path = "/home/$client->{user_name}/web";
my $vhost_path = "$web_path/$vhost_name";
# create web directory
if (!-d $web_path) {
mkdir $web_path, 0711 or return {
status => 403,
message => "Executor: cannot create directory $web_path"
};
}
# check if deleted vhost directory exists
my $deleted_vhost_path = "$vhost_path (deleted)";
if (-d $deleted_vhost_path and !-d $vhost_path) {
move($deleted_vhost_path, $vhost_path) or return {
status => 403,
message => "Executor: cannot move $deleted_vhost_path to $vhost_path"
};
}
# both deleted and new directory exists
elsif (-d $deleted_vhost_path and -d $vhost_path) {
return {
status => 304,
message => "Executor: both deleted and new vhost directory exists. Please clean up by yourself"
};
}
# create vhost directory
elsif (!-d $vhost_path) {
mkdir $vhost_path, 0711 or return {
status => 403,
message => "Executor: cannot create directory $vhost_path"
};
# create vhost subdirectories
for ( qw(htdocs logs conf) ) {
my $dir_name = $_;
mkdir "$vhost_path/$dir_name", 0711 or return {
status => 403,
message => "Executor: cannot create directory $vhost_path/$dir_name"
};
}
}
# apache2 config
my $apache2_config_file = "/etc/apache2/sites-available/$vhost_name";
if (!-f $apache2_config_file) {
my $apache2_config;
eval {
$apache2_config = template('apache2', {
vhost_name => $vhost_name,
vhost_path => $vhost_path,
});
}
or do {
return { status => 500, message => $@ };
};
open my $apache2_fh, '>', $apache2_config_file or die $!;
print $apache2_fh $apache2_config;
close $apache2_fh;
}
symlink("../sites-available/$vhost_name", "/etc/apache2/sites-enabled/$vhost_name") or die;
# nginx config
my $nginx_config_file = "/etc/nginx/sites-available/$vhost_name";
if (!-f $nginx_config_file) {
my $nginx_config;
eval {
$nginx_config = template('nginx', {
vhost_name => $vhost_name,
vhost_path => $vhost_path,
});
}
or do {
return { status => 500, message => $@ };
};
### Nginx config: $nginx_config
open my $nginx_fh, '>', $nginx_config_file or die;
print $nginx_fh $nginx_config;
close $nginx_fh;
}
symlink("../sites-available/$vhost_name", "/etc/nginx/sites-enabled/$vhost_name") or die;
return { status => 0, message => 'OK' };
}
exit;