-
Notifications
You must be signed in to change notification settings - Fork 3
/
save_server.cgi
184 lines (169 loc) · 4.3 KB
/
save_server.cgi
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
#!/usr/bin/perl
# Create, update or delete a virtual host
use strict;
use warnings;
require 'virtualmin-nginx-lib.pl';
our (%text, %in, %config, %access);
&ReadParse();
&error_setup($text{'server_err'});
$access{'edit'} || &error($text{'server_ecannotedit'});
# Get the current server
&lock_all_config_files();
my $conf = &get_config();
my $http = &find("http", $conf);
my @servers = &find("server", $http);
my $server;
my $old_name;
if ($in{'new'}) {
$access{'vhosts'} && &error($text{'server_ecannotcreate'});
$server = { 'name' => 'server',
'type' => 1,
'words' => [ ],
'members' => [ ] };
$server->{'file'} = &get_add_to_file($in{'server_name'});
}
else {
$server = &find_server($in{'id'});
$server || &error($text{'server_egone'});
&can_edit_server($server) || &error($text{'server_ecannot'});
$old_name = &find_value("server_name", $server);
}
# Check for clash
if ($in{'new'} || $in{'server_name'} ne $old_name) {
foreach my $c (@servers) {
my $cname = &find_value("server_name", $c);
$cname eq $in{'server_name'} &&
&error($text{'server_eclash'});
}
}
my $action;
my $name;
if ($in{'delete'}) {
$name = &find_value("server_name", $server);
if ($in{'confirm'}) {
# Got confirmation, delete it
&save_directive($http, [ $server ], [ ]);
$action = 'delete';
}
else {
# Ask for confirmation first
&ui_print_header(&server_desc($server),
$text{'server_edit'}, "");
print &ui_confirmation_form("save_server.cgi",
&text('server_rusure',
"<tt>".&html_escape($name)."</tt>"),
[ [ 'id', $in{'id'} ],
[ 'delete', 1 ] ],
[ [ 'confirm', $text{'server_confirm'} ] ],
);
&ui_print_footer("edit_server.cgi?id=".&urlize($in{'id'}),
$text{'server_return'});
}
}
else {
if ($in{'new'}) {
# Create a new server object
&save_directive($http, [ ], [ $server ]);
$action = 'create';
}
else {
$action = 'modify';
}
# Validate and update existing directives, starting with hostname
# or regexp
&nginx_text_parse("server_name", $server, undef, '^\S+$', undef, 1);
$name = $in{'server_name'};
# Get param for default server
my $ver = &get_nginx_version();
my $defserv;
if ($ver > 0.8 ||
$ver =~ /^0\.8\.(\d+)/ && $1 >= 21) {
$defserv = "default_server";
}
else {
$defserv = "default";
}
# Addresses to accept connections on
# XXX preserve existing args
my $i = 0;
my @listen;
while(defined($in{"ip_def_$i"})) {
my $def = $in{"ip_def_$i"};
if ($def == 3) {
$i++;
next;
}
my $ip;
if ($def == 0) {
$ip = $in{"ip_$i"};
$ip || &error(&text('server_eipmissing', $i+1));
&to_ipaddress($ip) || &to_ip6address($ip) ||
&error(&text('server_eip', $i+1, $ip));
if (&check_ip6address($ip)) {
$ip = "[$ip]";
}
}
elsif ($def == 2) {
$ip = "[::]";
}
# Port number
$in{"port_$i"} =~ /^\d+$/ ||
&error(&text('server_eport', $i+1));
if ($ip && $in{"port_$i"} != 80) {
$ip .= ":".$in{"port_$i"};
}
elsif (!$ip) {
$ip = $in{"port_$i"};
}
# Other random options
my @words = ( $ip );
if ($in{"default_$i"}) {
push(@words, $defserv);
}
if ($in{"ssl_$i"}) {
push(@words, "ssl");
}
if ($in{"ipv6_$i"}) {
push(@words, "ipv6only=".$in{"ipv6_$i"});
}
push(@listen, { 'name' => 'listen',
'value' => $words[0],
'words' => \@words });
$i++;
}
@listen || &error($text{'server_elisten'});
&save_directive($server, "listen", \@listen);
if ($in{'new'}) {
# Add root directory block
$in{'rootdir'} =~ /^\// || &error($text{'server_erootdir'});
-d $in{'rootdir'} || &error(&text('server_erootdir2',
$in{'rootdir'}));
&can_directory($in{'rootdir'}) ||
&error(&text('location_ecannot',
"<tt>".&html_escape($in{'rootdir'})."</tt>",
"<tt>".&html_escape($access{'root'})."</tt>"));
&save_directive($server, [ ],
[ { 'name' => 'location',
'words' => [ '/' ],
'type' => 1,
'members' => [
{ 'name' => 'root',
'words' => [ $in{'rootdir'} ] },
],
} ]);
&save_directive($server, "root", $in{'rootdir'});
}
}
&flush_config_file_lines();
&unlock_all_config_files();
if ($action eq 'create') {
&create_server_link($server);
}
elsif ($action eq 'delete') {
&delete_server_link($server);
&delete_server_file_if_empty($server);
}
if ($action) {
&webmin_log($action, 'server', $name);
&redirect("");
}