forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_shared.cgi
executable file
·231 lines (218 loc) · 6.16 KB
/
save_shared.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
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
#!/usr/local/bin/perl
# save_shared.cgi
# Update, create or delete a shared network
require './dhcpd-lib.pl';
require './params-lib.pl';
&ReadParse();
&lock_file($config{'dhcpd_conf'});
($par, $sha, $indent) = &get_branch('sha', $in{'new'});
$parconf = $par->{'members'};
# check acls
%access = &get_module_acl();
&error_setup($text{'eacl_aviol'});
if ($in{'delete'}) {
&error("$text{'eacl_np'} $text{'eacl_pdn'}")
if !&can('rw', \%access, $sha, 1);
}
elsif ($in{'options'}) {
&error("$text{'eacl_np'} $text{'eacl_psn'}")
if !&can('r', \%access, $sha);
}
elsif ($in{'new'}) {
&error("$text{'eacl_np'} $text{'eacl_pin'}")
unless &can('c', \%access, $sha) && &can('rw', \%access, $par);
# restrict duplicates
if($access{'uniq_sha'}) {
foreach $s (&find("shared-network", &get_config())) {
&error("$text{'eacl_np'} $text{'eacl_uniq'}")
if lc $s->{'values'}->[0] eq lc $in{'name'};
}
}
}
else {
&error("$text{'eacl_np'} $text{'eacl_pun'}")
if !&can('rw', \%access, $sha);
}
# save
if ($in{'options'}) {
# Redirect to client options
&redirect("edit_options.cgi?idx=$in{'idx'}");
exit;
}
else {
if ($in{'delete'}) {
&error_setup($text{'sshared_faildel'});
}
else {
&error_setup($text{'sshared_failsave'});
$in{'name'} =~ /^\S+$/ ||
&error($text{'sshared_invalidsname'});
$sha->{'values'} = [ $in{'name'} ];
$sha->{'comment'} = $in{'desc'};
}
# Move hosts, groups and subnets into or out of this shared network
@wasin = &find("host", $sha->{'members'});
foreach $hn (split(/\0/, $in{'hosts'})) {
if ($hn =~ /(\d+),(\d+)/) {
push(@nowin, $parconf->[$2]->{'members'}->[$1]);
$nowpr{$parconf->[$2]->{'members'}->[$1]} =
$parconf->[$2];
}
elsif ($hn =~ /(\d+),/) {
push(@nowin, $parconf->[$1]);
$nowpr{$parconf->[$1]} = $par;
}
if ($nowin[$#nowin]->{'name'} ne "host") {
&error($text{'sgroup_echanged'});
}
}
@wasgin = &find("group", $sha->{'members'});
foreach $gn (split(/\0/, $in{'groups'})) {
if ($gn =~ /(\d+),(\d+)/) {
push(@nowgin, $parconf->[$2]->{'members'}->[$1]);
$nowgpr{$parconf->[$2]->{'members'}->[$1]} =
$parconf->[$2];
}
elsif ($gn =~ /(\d+),/) {
push(@nowgin, $parconf->[$1]);
$nowgpr{$parconf->[$1]} = $par;
}
if ($nowgin[$#nowgin]->{'name'} ne "group") {
&error($text{'sgroup_echanged'});
}
}
@wasuin = &find("subnet", $sha->{'members'});
foreach $un (split(/\0/, $in{'subnets'})) {
if ($un =~ /(\d+),(\d+)/) {
push(@nowuin, $parconf->[$2]->{'members'}->[$1]);
$nowupr{$parconf->[$2]->{'members'}->[$1]} =
$parconf->[$2];
}
elsif ($un =~ /(\d+),/) {
push(@nowuin, $parconf->[$1]);
$nowupr{$parconf->[$1]} = $par;
}
if ($nowuin[$#nowuin]->{'name'} ne "subnet") {
&error($text{'sgroup_echanged'});
}
}
&error_setup($text{'eacl_aviol'});
foreach $h (&unique(@wasin, @nowin)) {
$was = &indexof($h, @wasin) != -1;
$now = &indexof($h, @nowin) != -1;
# per-host ACLs for new or updated hosts
if ($was != $now && !&can('rw', \%access, $h)) {
&error("$text{'eacl_np'} $text{'eacl_pun'}");
}
if ($was && !$now) {
# Move out of the shared network
&save_directive($sha, [ $h ], [ ], 0);
&save_directive($par, [ ], [ $h ], 0);
}
elsif ($now && !$was) {
# Move into the shared network (maybe from another)
&save_directive($nowpr{$h}, [ $h ], [ ], 0);
&save_directive($sha, [ ], [ $h ], 1);
}
}
foreach $g (&unique(@wasgin, @nowgin)) {
$was = &indexof($g, @wasgin) != -1;
$now = &indexof($g, @nowgin) != -1;
# per-group ACLs for new or updated groups
if ($was != $now && !&can('rw', \%access, $g)) {
&error("$text{'eacl_np'} $text{'eacl_pun'}");
}
if ($was && !$now) {
# Move out of the shared network
&save_directive($sha, [ $g ], [ ], 0);
&save_directive($par, [ ], [ $g ], 0);
}
elsif ($now && !$was) {
# Move into the shared network (maybe from another)
&save_directive($nowgpr{$g}, [ $g ], [ ], 0);
&save_directive($sha, [ ], [ $g ], 1);
}
}
foreach $u (&unique(@wasuin, @nowuin)) {
$was = &indexof($u, @wasuin) != -1;
$now = &indexof($u, @nowuin) != -1;
# per-subnet ACLs for new or updated subnetss
if ($was != $now && !&can('rw', \%access, $u)) {
&error("$text{'eacl_np'} $text{'eacl_pun'}");
}
if ($was && !$now) {
# Move out of the shared network
&save_directive($sha, [ $u ], [ ], 0);
&save_directive($par, [ ], [ $u ], 0);
if ($par->{'name'} eq "shared-network") {
&fix_sequence($par);
}
}
elsif ($now && !$was) {
# Move into the shared network (maybe from another)
&save_directive($nowupr{$u}, [ $u ], [ ], 0);
&save_directive($sha, [ ], [ $u ], 1);
if ($nowupr{$u}->{'name'} eq "shared-network") {
&check_subnets($nowupr{$u});
}
}
}
&check_subnets($sha);
&fix_sequence($sha);
if (!$in{'delete'}) {
&parse_params($sha);
if ($in{'new'}) {
# Add this shared net
&save_directive($par, [ ], [ $sha ], 0);
}
else {
# Update shared net
&save_directive($par, [ $sha ], [ $sha ], 0);
}
}
}
&flush_file_lines();
if ($in{'delete'}) {
# Delete this net
if ($in{'hosts'} eq "" && $in{'groups'} eq "" && $in{'subnets'} eq "") {
&save_directive($par, [ $sha ], [ ], 0);
&flush_file_lines();
}
else {
&unlock_file($config{'dhcpd_conf'});
&redirect("confirm_delete.cgi?idx=$in{'idx'}\&type=0");
exit;
}
}
&unlock_file($config{'dhcpd_conf'});
&webmin_log($in{'delete'} ? 'delete' : $in{'new'} ? 'create' : 'modify',
'shared', $sha->{'values'}->[0], \%in);
&redirect("");
# check whether thist shared network contains any subnet
sub check_subnets
{
local(@subnets);
@subnets = &find("subnet", $_[0]->{'members'});
if (@subnets == 0) {
&error_setup($text{'sshared_failsave'});
&error(&text('sshared_nosubnet', $_[0]->{'values'}->[0]));
}
}
# force hosts and groups to follow subnets
sub fix_sequence
{
local(@subnets, $max, $u, $i);
@subnets = &find("subnet", $_[0]->{'members'});
$max = -1;
foreach $u (@subnets) {
$max = $u->{'index'} > $max ? $u->{'index'} : $max;
}
for ($i = 0; $i < $max; $i++) {
$u = $_[0]->{'members'}->[$i];
if ($u->{'name'} eq "host" || $u->{'name'} eq "group") {
# move to the end of list
&save_directive($_[0], [ $u ], [ ], 0);
&save_directive($_[0], [ ], [ $u ], 0);
}
}
}