blib/lib/App/Netdisco/Web/AdminTask.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 71 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 319 CC 18 SCAR 45.1
CC
18
Cov
0%
add_job
182.0
single_address
30.0
nonadmin_job_target
6.0
CRAP 90.9
1 package App::Netdisco::Web::AdminTask;
2  
3 use Dancer ':syntax';
4 use Dancer::Plugin::Ajax;
5 use Dancer::Plugin::DBIC;
6 use Dancer::Plugin::Auth::Extensible;
7  
8 use NetAddr::IP qw/:rfc3021 :lower/;
9 use Socket qw/inet_pton AF_INET AF_INET6/;
10 use App::Netdisco::JobQueue 'jq_insert';
11 use App::Netdisco::Util::JobAction 'action_is_refused';
12  
13 sub add_job {
14 my ($action, $device, $extra, $port) = @_;
15  
16 # a false return is how every caller already reports a job not queued
17 return if action_is_refused($action);
18  
19 my $net = NetAddr::IP->new($device);
20 return if
21 ($device and (!$net or $net->num == 0 or $net->addr eq '0.0.0.0'));
22 return if
23 (($action eq 'discover' or $action eq 'pingsweep') and $device and
24 (($net->version == 6 and $net->masklen != 128)
25 or ($net->version == 4 and $net->masklen < 22)));
26  
27 my @hostlist = $device ? ($net->hostenum) : (undef);
28 @hostlist = ($device) if $action eq 'pingsweep';
29  
30 my $happy = jq_insert([map {{
31 action => $action,
32 ($_ ? (device => ($action eq 'pingsweep' ? $_ : $_->addr)) : ()),
33 ($port ? (port => $port) : ()),
34 ($extra ? (extra => $extra) : ()),
35 username => session('logged_in_user'),
36 userip => scalar eval {request->remote_address},
37 }} @hostlist]);
38  
39 foreach my $h (@hostlist) {
40 next unless defined $h;
41 my $msg = ($happy ? "Queued job to $action device \%s"
42 : "Failed to queue job to $action device \%s");
43  
44 schema(vars->{'tenant'})->resultset('UserLog')->create({
45 username => session('logged_in_user'),
46 userip => scalar eval {request->remote_address},
47 event => (sprintf $msg, ($action eq 'pingsweep' ? $h : $h->addr)),
48 details => ($extra || 'no user log supplied'),
49 });
50 }
51  
52 return $happy;
53 }
54  
55 # a job requested by a non-admin user may only be aimed at one device which is
56 # already in the inventory
57 sub nonadmin_job_target {
58 my $device = shift;
59 my $addr = single_address($device) or return undef;
60  
61 return schema(vars->{'tenant'})->resultset('Device')
62 ->search({ip => $addr})->count ? $addr : undef;
63 }
64  
65 sub single_address {
66 my $device = shift;
67 return undef unless defined $device and length $device;
68  
69 # a name is resolved again when the job is queued, and may resolve then to
70 # an address other than the one checked here
71 my ($host) = split m{/}, $device, 2;
72 return undef unless inet_pton(AF_INET, $host) or inet_pton(AF_INET6, $host);
73  
74 my $net = NetAddr::IP->new($device) or return undef;
75 return undef
76 if ($net->version == 4 ? $net->masklen != 32 : $net->masklen != 128);
77  
78 return $net->addr;
79 }
80  
81 foreach my $action (@{ setting('job_prio')->{high} },
82 @{ setting('job_prio')->{normal} }) {
83  
84 next if $action and $action =~ m/^hook::/; # skip hooks
85  
86 ajax "/ajax/control/admin/$action" => require_role admin => sub {
87 add_job($action, param('device'), param('extra'), param('port'))
88 or send_error('Bad device', 400);
89 };
90  
91 post "/admin/$action" => require_role admin => sub {
92 add_job($action, param('device'), param('extra'), param('port'))
93 ? redirect uri_for('/admin/jobqueue')->path
94 : redirect uri_for('/')->path;
95 };
96 }
97  
98 if (setting('enable_nonadmin_actions')) {
99 # the backend merges a job's extra and port into its own running
100 # configuration, so only an admin may send them
101 foreach my $action (@{ setting('nonadmin_actions') }) {
102 ajax "/ajax/control/nonadmin/$action" => sub {
103 send_error('Bad device', 400)
104 unless nonadmin_job_target(param('device'));
105 add_job($action, param('device'))
106 or send_error('Bad device', 400);
107 };
108 post "/nonadmin/$action" => sub {
109 return redirect uri_for('/device', {tab => 'details', q => param('device')})
110 unless nonadmin_job_target(param('device'));
111 add_job($action, param('device'));
112 redirect uri_for('/device', {tab => 'details', q => param('device')});
113 };
114 }
115 }
116  
117 post "/admin/discodevs" => require_role admin => sub {
118 add_job((param('action') || 'discover'), param('device'), (param('timeout') || param('extra')), param('port'))
119 ? redirect uri_for('/admin/jobqueue')->path
120 : redirect uri_for('/')->path;
121 };
122  
123 ajax qr{/ajax/control/admin/(?:\w+/)?renumber} => require_role admin => sub {
124 send_error('Missing device', 400) unless param('device');
125 send_error('Missing new IP', 400) unless param('newip');
126  
127 my $device = NetAddr::IP->new(param('device'));
128 send_error('Bad device', 400)
129 if ! $device or $device->addr eq '0.0.0.0';
130  
131 my $newip = NetAddr::IP->new(param('newip'));
132 send_error('Bad new IP', 400)
133 if ! $newip or $newip->addr eq '0.0.0.0';
134  
135 my $happy = jq_insert([{
136 action => 'renumber',
137 device => $device->addr,
138 extra => $newip->addr,
139 username => session('logged_in_user'),
140 userip => scalar eval {request->remote_address},
141 }]);
142  
143 my $msg = ($happy ? 'Queued job to renumber device %s to %s'
144 : 'Failed to queue job to renumber device %s to %s');
145  
146 schema(vars->{'tenant'})->resultset('UserLog')->create({
147 username => session('logged_in_user'),
148 userip => scalar eval {request->remote_address},
149 event => (sprintf $msg, $device->addr, $newip->addr),
150 });
151  
152 return $happy;
153 };
154  
155 ajax "/ajax/control/admin/snapshot_req" => require_role admin => sub {
156 my $device = NetAddr::IP->new(param('device'));
157 send_error('Bad device', 400)
158 if ! $device or $device->addr eq '0.0.0.0';
159  
160 # will store for download, and for browsing only if loadmibs has been run
161 add_job('snapshot', $device->addr) or send_error('Bad device', 400);
162 };
163  
164 get "/ajax/content/admin/snapshot_get" => require_role admin => sub {
165 my $device = NetAddr::IP->new(param('device'));
166 send_error('Bad device', 400)
167 if ! $device or $device->addr eq '0.0.0.0';
168  
169 my @rows = schema(vars->{'tenant'})->resultset('DeviceBrowser')
170 ->search({
171 ip => $device->addr,
172 -bool => \q{ jsonb_typeof(value) = 'array' },
173 })->hri->all;
174  
175 send_error('No snapshot', 400)
176 if 0 == scalar @rows;
177  
178 my @snmpwalk = ();
179 foreach my $row (@rows) {
180 $row->{value} = (@{ from_json($row->{value}) })[0];
181 if (ref {} eq ref $row->{value}) {
182 foreach my $k (keys %{ $row->{value} }) {
183 push @snmpwalk, [($row->{oid} .'.'. $k), $row->{value}->{$k}];
184 }
185 }
186 else {
187 push @snmpwalk, [$row->{oid}, $row->{value}];
188 }
189 }
190  
191 # .1.3.6.1.2.1.25.5.1.1.1.38441 = INTEGER: 40
192 my $content = join "\n",
193 map {sprintf '%s = BASE64: %s', $_->[0], $_->[1]} @snmpwalk;
194 $content .= "\n";
195  
196 send_file( \$content, content_type => 'text/plain', filename => ($device->addr .'-snapshot.txt') );
197 };
198  
199 ajax "/ajax/control/admin/snapshot_del" => require_role setting('defanged_admin') => sub {
200 my $device = NetAddr::IP->new(param('device'));
201 send_error('Bad device', 400)
202 if ! $device or $device->addr eq '0.0.0.0';
203  
204 schema(vars->{'tenant'})->resultset('DeviceBrowser')->search({ip => $device->addr})->delete;
205 };
206  
207 true;