-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeletehost.cgi
executable file
·340 lines (279 loc) · 7.67 KB
/
deletehost.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/local/bin/perl -w
#
# $Id$
#
# cgi-script to delete host objects
#
use strict;
use HOSTDB;
use SUCGI2;
my $table_blank_line = "<tr><td COLSPAN='2'> </td></tr>\n";
my $table_hr_line = "<tr><td COLSPAN='2'><hr></td></tr>\n";
my $debug = 0;
if (defined($ARGV[0]) and $ARGV[0] eq "-d") {
shift (@ARGV);
$debug = 1;
}
my $hostdbini = Config::IniFiles->new (-file => HOSTDB::get_inifile ());
my $sucgi_ini;
if (-f $hostdbini->val ('sucgi', 'cfgfile')) {
$sucgi_ini = Config::IniFiles->new (-file => $hostdbini->val ('sucgi', 'cfgfile'));
} else {
warn ("No SUCGI config-file ('" . $hostdbini->val ('sucgi', 'cfgfile') . "')");
}
my $q = SUCGI2->new ($sucgi_ini, 'hostdb');
$q->begin (title => 'Delete Host');
my $hostdb = eval {
HOSTDB::DB->new (ini => $hostdbini, debug => $debug);
};
if ($@) {
my $e = $@;
$q->print (" <p><ul><font COLOR='red' SIZE='3'><strong>Could not create HOSTDB object: $e</strong></font></ul>\n\n");
$q->end ();
die ("$0: Could not create HOSTDB object: '$e'");
}
my %links = $hostdb->html_links ($q);
my $remote_user = $q->user();
unless ($remote_user) {
$q->print (" <p><ul><font COLOR='red' SIZE='3'><strong>You are not logged in.</strong></font></ul>\n\n");
$q->end ();
die ("$0: Invalid REMOTE_USER environment variable '$ENV{REMOTE_USER}'");
}
my $is_admin = $hostdb->auth->is_admin ($remote_user);
my $is_helpdesk = $hostdb->auth->is_helpdesk ($remote_user);
my (@links, @admin_links);
push (@admin_links, "[<a HREF='$links{netplan}'>netplan</a>]") if ($is_admin and $links{netplan});
push (@links, "[<a HREF='$links{home}'>home</a>]") if ($links{home});
push (@links, "[<a HREF='$links{whois}'>whois</a>]") if ($links{whois});
my $l = '';
if (@links or @admin_links) {
$l = join(' ', @links, @admin_links);
}
$q->print (<<EOH);
<table BORDER='0' CELLPADDING='0' CELLSPACING='3' WIDTH='100%'>
$table_blank_line
<tr>
<td ALIGN='center'>
<h3>HOSTDB: Delete Host</h3>
</td>
<td ALIGN='right'>$l</td>
</tr>
$table_blank_line
EOH
my $action = $q->param('action');
$action = 'Search' unless $action;
SWITCH:
{
my $id = $q->param('id');
my $host;
$host = $hostdb->findhostbyid ($id);
error_line ($q, "$0: Could not find host object with ID '$id'\n"), last SWITCH unless (defined ($host));
$action eq 'Delete' and do
{
my $ip = $host->ip ();
# get subnet
my $subnet = $hostdb->findsubnetbyip ($host->ip ());
# get zone
my $zone = $hostdb->findzonebyhostname ($host->hostname ());
# check that user is allowed to edit both zone and subnet
my $authorized = 1;
if (! $is_admin and ! $is_helpdesk) {
if (! defined ($subnet) or ! $hostdb->auth->is_allowed_write ($subnet, $remote_user)) {
error_line ($q, "You do not have sufficient access to subnet '" . $subnet->subnet () . "'");
}
# if there is no zone, only base desicion on subnet rights
if (defined ($zone) and ! $hostdb->auth->is_allowed_write ($zone, $remote_user)) {
error_line ($q, "You do not have sufficient access to zone '" . $zone->zone () . "'");
$authorized = 0;
}
}
if ($authorized) {
my $identify_str = "id:'" . ($host->id () || 'no id') . "' hostname:'" . ($host->hostname () || 'no hostname') . "' ip:'" . ($host->ip () || 'no ip') . "'";
if (delete_host ($hostdb, $host, $q)) {
my $i = localtime () . " deletehost.cgi[$$]";
warn ("$i User '$remote_user' (from $ENV{REMOTE_ADDR}) deleted the following host -- $identify_str\n");
my @links;
$q->print (<<EOH);
<tr>
<td COLSPAN='2'><strong><font COLOR='red'>Host deleted</font></strong></td>
</tr>
EOH
if (defined ($subnet) and $links{showsubnet}) {
my $s = $subnet->subnet ();
my $link = "<a HREF='$links{showsubnet};subnet=$s'>Show subnet</a>";
push (@links, <<EOH);
<tr>
<td COLSPAN='2'> [$link $s]<br></td>
</tr>
EOH
}
if ($links{modifyhost}) {
$ip = "<a HREF='$links{modifyhost};ip=$ip'>New host</a> with IP $ip";
push (@links, <<EOH);
<tr>
<td COLSPAN='2'> [$ip]</td>
</tr>
EOH
}
if (@links) {
$q->print (<<EOH);
$table_blank_line
<tr>
<td COLSPAN='2'><strong>Courtesy links :</td>
</tr>
@links
EOH
}
} else {
error_line ($q, "Delete failed: $host->{error}");
}
}
},last SWITCH;
print_host_info ($q, $hostdb, $host);
delete_form ($q, $host);
}
if ($@) {
error_line($q, "$@\n");
}
$q->print (<<EOH);
</table>
EOH
$q->end();
sub delete_host
{
my $hostdb = shift;
my $host = shift;
my $q = shift;
if ($q->param ("_hostdb.deletehost") ne "yes") {
error_line ($q, "Delete without verification not supported, don't try to trick me.");
return undef;
}
eval {
die ("No host object") unless ($host);
$host->delete ("YES") or die ($host->{error});
};
if ($@) {
chomp ($@);
error_line ($q, "Failed to delete host: $@");
return 0;
}
return 1;
}
sub delete_form
{
my $q = shift;
my $host = shift;
# HTML
my $state_field = $q->state_field ();
my $delete = $q->submit (-name=>'action', -value=>'Delete',-class=>'button');
my $me = $q->state_url ();
my $id = $host->id ();
$q->print (<<EOH);
<tr>
<td ALIGN='right'><font COLOR='red'><strong>Are you SURE you want to delete this host?</strong></font></td>
<td ALIGN='right'>
<form ACTION='$me' METHOD='post'>
$state_field
<input TYPE='hidden' NAME='id' VALUE='$id'>
<input TYPE='hidden' NAME='_hostdb.deletehost' VALUE='yes'>
$delete
</form>
</td>
</tr>
$table_blank_line
EOH
return 1;
}
sub print_host_info
{
my $q = shift;
my $hostdb = shift;
my $host = shift;
return undef if (! defined ($host));
# HTML
my $me = $q->state_url();
my $id = $host->id ();
my $parent = $host->partof ()?$host->partof ():'-';
$parent = "<a href='$links{whois};whoisdatatype=ID;whoisdata=$parent'>$parent</a>" if ($parent ne '-' and $links{whois});
my $ip = $host->ip ();
my $mac = $host->mac_address () || '';
my $hostname = $host->hostname ();
my $comment = $host->comment () || '';
my $owner = $host->owner ();
# get subnet
my $subnet = $hostdb->findsubnetbyip ($host->ip () || $q->param ('ip'));
my $subnet_link = 'unknown';
if (defined ($subnet)) {
$subnet_link = $subnet->subnet ();
$subnet_link = "<a HREF='$links{showsubnet};subnet=$subnet_link'>$subnet_link</a>" if ($links{showsubnet});
}
$q->print (<<EOH);
<tr>
<td>ID</td>
<td><a HREF="$links{whois};whoisdatatype=ID;whoisdata=$id">$id</a> </td>
</tr>
<tr>
<td>Parent</td>
<td>$parent</td>
</tr>
EOH
my $t_host;
foreach $t_host ($hostdb->findhostbypartof ($id)) {
my $child = $t_host->id ()?$t_host->id ():'-';
$child = "<a HREF='$me;whoisdatatype=ID;whoisdata=$child'>$child</a>";
$q->print (<<EOH);
<tr>
<td>Child</td>
<td>$child</td>
</tr>
EOH
}
$q->print (<<EOH);
<tr>
<td ALIGN='center'>---</td>
<td> </td>
</tr>
<tr>
<td>IP address</td>
<td><strong>$ip</strong></td>
</tr>
<tr>
<td>Subnet</td>
<td>$subnet_link</td>
</tr>
<tr>
<td>MAC Address</td>
<td>$mac</td>
</tr>
<tr>
<td>Hostname</td>
<td><strong>$hostname</strong></td>
</tr>
<tr>
<td>Comment</td>
<td>$comment</td>
</tr>
<tr>
<td>Owner</td>
<td>$owner</td>
</tr>
EOH
return 1;
}
sub error_line
{
my $q = shift;
my $error = shift;
chomp ($error);
$q->print (<<EOH);
<tr>
<td COLSPAN='4'>
<font COLOR='red'>
<strong>$error</strong>
</font>
</td>
</tr>
EOH
my $i = localtime () . " deletehost.cgi[$$]";
warn ("$i: $error\n");
}