| 1 | package App::Netdisco::Util::Port; | ||
| 2 | |||
| 3 | use Dancer qw/:syntax :script/; | ||
| 4 | use Dancer::Plugin::DBIC 'schema'; | ||
| 5 | use Dancer::Plugin::Auth::Extensible; | ||
| 6 | |||
| 7 | use App::Netdisco::Util::Device 'get_device'; | ||
| 8 | use App::Netdisco::Util::Permission qw/acl_matches acl_matches_only/; | ||
| 9 | |||
| 10 | use base 'Exporter'; | ||
| 11 | our @EXPORT = (); | ||
| 12 | our @EXPORT_OK = qw/ | ||
| 13 | port_acl_service port_acl_pvid port_acl_name | ||
| 14 | port_acl_check port_acl_by_role_check device_acl_by_role_check | ||
| 15 | get_port get_iid get_powerid | ||
| 16 | is_vlan_subinterface port_has_phone port_has_wap | ||
| 17 | to_speed | ||
| 18 | /; | ||
| 19 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | App::Netdisco::Util::Port | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | A set of helper subroutines to support parts of the Netdisco application. | ||
| 28 | |||
| 29 | There are no default exports, however the C<:all> tag will export all | ||
| 30 | subroutines. | ||
| 31 | |||
| 32 | =head1 EXPORT_OK | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | # the role names a user holds. a role is a name in portctl_by_role, and an | ||
| 37 | # empty list means port_control is not scoped to any role | ||
| 38 | sub _user_portctl_roles { | ||
| 39 | my $user = shift; | ||
| 40 | my $roles = $user->portctl_roles; | ||
| 41 | return () unless $roles and ref $roles eq ref []; | ||
| 42 | return grep { defined and length } @$roles; | ||
| 43 | } | ||
| 44 | |||
| 45 | # one role ACL against a device and port. a device ACL permits every port on a | ||
| 46 | # matching device, and a hash of device => port pairs needs both to match | ||
| 47 | sub _role_allows_port { | ||
| 48 | my ($acl, $device, $port) = @_; | ||
| 49 | |||
| 50 | if (ref $acl eq q{} or ref $acl eq ref []) { | ||
| 51 | return acl_matches($device, $acl) ? true : false; | ||
| 52 | } | ||
| 53 | elsif (ref $acl eq ref {}) { | ||
| 54 | foreach my $key (keys %$acl) { | ||
| 55 | # lhs matches device, rhs matches port | ||
| 56 | next unless $key and $acl->{$key}; | ||
| 57 | return true if (acl_matches($device, $key) | ||
| 58 | and acl_matches_only($port, $acl->{$key})); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | return false; | ||
| 62 | } | ||
| 63 | |||
| 64 | # for changes with no port to match so the device must be matched | ||
| 65 | sub _role_allows_device { | ||
| 66 | my ($acl, $device) = @_; | ||
| 67 | |||
| 68 | if (ref $acl eq q{} or ref $acl eq ref []) { | ||
| 69 | return acl_matches($device, $acl) ? true : false; | ||
| 70 | } | ||
| 71 | elsif (ref $acl eq ref {}) { | ||
| 72 | foreach my $key (grep { defined } keys %$acl) { | ||
| 73 | return true if acl_matches($device, $key); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | return false; | ||
| 77 | } | ||
| 78 | |||
| 79 | =head2 port_acl_by_role_check( $port, $device?, $user? ) | ||
| 80 | |||
| 81 | =over 4 | ||
| 82 | |||
| 83 | =item * | ||
| 84 | |||
| 85 | Permission check on C<portctl_by_role> if the device and user are provided. A | ||
| 86 | bare username will be promoted to a user instance. | ||
| 87 | |||
| 88 | A user may hold several roles and is permitted what any one of them permits. | ||
| 89 | A role which is not defined grants nothing, and a user whose roles are all | ||
| 90 | undefined is refused. | ||
| 91 | |||
| 92 | =back | ||
| 93 | |||
| 94 | Will return false if these checks fail, otherwise true. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub port_acl_by_role_check { | ||
| 99 | my ($port, $device, $user) = @_; | ||
| 100 | |||
| 101 | # skip user acls for netdisco-do --force jobs | ||
| 102 | # this avoids the need to create a netdisco user in the DB and give rights | ||
| 103 | return true if $ENV{ND2_DO_FORCE}; | ||
| 104 | |||
| 105 | # portctl_by_role check | ||
| 106 | if ($device and ref $device and $user) { | ||
| 107 | $user = ref $user ? $user : | ||
| 108 | schema('netdisco')->resultset('User') | ||
| 109 | ->find({ username => $user }); | ||
| 110 | return false unless $user; | ||
| 111 | |||
| 112 | # special case admin user allowed to continue, because | ||
| 113 | # they can submit port control jobs | ||
| 114 | return true if ($user->admin and $user->port_control); | ||
| 115 | |||
| 116 | my @roles = _user_portctl_roles($user); | ||
| 117 | |||
| 118 | # a user holds the union of their roles' rights, and a role missing | ||
| 119 | # from portctl_by_role grants nothing | ||
| 120 | foreach my $role (@roles) { | ||
| 121 | my $acl = setting('portctl_by_role')->{$role} or next; | ||
| 122 | return true if _role_allows_port($acl, $device, $port); | ||
| 123 | } | ||
| 124 | |||
| 125 | # roles were assigned but none of them allowed this, so the user must not fall back to any-port | ||
| 126 | return false if scalar @roles; | ||
| 127 | |||
| 128 | # if the user has "Enabled (any port)" setting | ||
| 129 | return $user->port_control; | ||
| 130 | } | ||
| 131 | |||
| 132 | return false; | ||
| 133 | } | ||
| 134 | |||
| 135 | =head2 port_acl_check( $port, $device?, $user? ) | ||
| 136 | |||
| 137 | =over 4 | ||
| 138 | |||
| 139 | =item * | ||
| 140 | |||
| 141 | Permission check that C<portctl_no> and C<portctl_only> pass for the device. | ||
| 142 | |||
| 143 | =back | ||
| 144 | |||
| 145 | Will return false if these checks fail, otherwise true. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | sub port_acl_check { | ||
| 150 | my ($port, $device, $user) = @_; | ||
| 151 | my $ip = $port->ip; | ||
| 152 | |||
| 153 | # check for limits on devices | ||
| 154 | return false if acl_matches($ip, 'portctl_no'); | ||
| 155 | return false unless acl_matches_only($ip, 'portctl_only'); | ||
| 156 | |||
| 157 | return true; | ||
| 158 | } | ||
| 159 | |||
| 160 | =head2 port_acl_service( $port, $device?, $user? ) | ||
| 161 | |||
| 162 | Checks if admin up/down or PoE status on a port can be changed. | ||
| 163 | |||
| 164 | Returns false if the request should be denied, true if OK to proceed. | ||
| 165 | |||
| 166 | First checks C<portctl_nameonly>, C<portctl_uplinks>, C<portctl_nowaps>, and | ||
| 167 | C<portctl_nophones>. | ||
| 168 | |||
| 169 | Then checks according to C<port_acl_check> and C<port_acl_by_role_check> above. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | sub port_acl_service { | ||
| 174 | my ($port, $device, $user) = @_; | ||
| 175 | |||
| 176 | return false if setting('portctl_nameonly'); | ||
| 177 | |||
| 178 | return false if setting('portctl_nowaps') and port_has_wap($port); | ||
| 179 | return false if setting('portctl_nophones') and port_has_phone($port); | ||
| 180 | |||
| 181 | return false if (not setting('portctl_uplinks')) and | ||
| 182 | (($port->is_uplink or $port->remote_type | ||
| 183 | or $port->is_master or is_vlan_subinterface($port)) and not | ||
| 184 | (port_has_wap($port) or port_has_phone($port))); | ||
| 185 | |||
| 186 | return false if not port_acl_check(@_); | ||
| 187 | return port_acl_by_role_check(@_); | ||
| 188 | } | ||
| 189 | |||
| 190 | =head2 port_acl_pvid( $port, $device?, $user? ) | ||
| 191 | |||
| 192 | Checks if native vlan (pvid) on a port can be changed. | ||
| 193 | |||
| 194 | Returns false if the request should be denied, true if OK to proceed. | ||
| 195 | |||
| 196 | First checks C<portctl_native_vlan>; | ||
| 197 | |||
| 198 | Then checks according to C<port_acl_service>. | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | sub port_acl_pvid { | ||
| 203 | my ($port, $device, $user) = @_; | ||
| 204 | |||
| 205 | return false unless setting('portctl_native_vlan'); | ||
| 206 | return port_acl_service(@_); | ||
| 207 | } | ||
| 208 | |||
| 209 | =head2 device_acl_by_role_check( $device, $user? ) | ||
| 210 | |||
| 211 | Permission check on C<portctl_by_role> for a change which belongs to the | ||
| 212 | device rather than to one of its ports, such as its location or contact. | ||
| 213 | |||
| 214 | A role ACL naming devices and ports is matched on the device alone, because | ||
| 215 | there is no port to match. This is the same decision the device details view | ||
| 216 | makes when it chooses whether to render an editable cell. | ||
| 217 | |||
| 218 | As for ports, a user holding several roles is permitted what any one permits. | ||
| 219 | |||
| 220 | Returns false if the request should be denied, true if OK to proceed. | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | sub device_acl_by_role_check { | ||
| 225 | my ($device, $user) = @_; | ||
| 226 | |||
| 227 | # skip user acls for netdisco-do --force jobs, as port_acl_by_role_check does | ||
| 228 | return true if $ENV{ND2_DO_FORCE}; | ||
| 229 | return false unless $device and $user; | ||
| 230 | |||
| 231 | $user = ref $user ? $user : | ||
| 232 | schema('netdisco')->resultset('User') | ||
| 233 | ->find({ username => $user }); | ||
| 234 | return false unless $user; | ||
| 235 | return false unless $user->port_control; | ||
| 236 | return true if $user->admin; | ||
| 237 | my @roles = _user_portctl_roles($user); | ||
| 238 | return true unless scalar @roles; | ||
| 239 | |||
| 240 | foreach my $role (@roles) { | ||
| 241 | my $acl = setting('portctl_by_role')->{$role} or next; | ||
| 242 | return true if _role_allows_device($acl, $device); | ||
| 243 | } | ||
| 244 | |||
| 245 | # assigned an unknown role or a role that does not work for this device | ||
| 246 | return false; | ||
| 247 | } | ||
| 248 | |||
| 249 | =head2 port_acl_name( $port, $device?, $user? ) | ||
| 250 | |||
| 251 | Checks if name (description) on a port can be changed. | ||
| 252 | |||
| 253 | Returns false if the request should be denied, true if OK to proceed. | ||
| 254 | |||
| 255 | Only setting C<portctl_by_role> is checked. | ||
| 256 | |||
| 257 | =cut | ||
| 258 | |||
| 259 | sub port_acl_name { goto &port_acl_by_role_check } | ||
| 260 | |||
| 261 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 262 | |||
| 263 | =head2 get_port( $device, $portname ) | ||
| 264 | |||
| 265 | Given a device IP address and a port name, returns a L<DBIx::Class::Row> | ||
| 266 | object for the Port on the Device in the Netdisco database. | ||
| 267 | |||
| 268 | The device IP can also be passed as a Device C<DBIx::Class> object. | ||
| 269 | |||
| 270 | Returns C<undef> if the device or port are not known to Netdisco. | ||
| 271 | |||
| 272 | Returns C<($device_instance, $port_instance)> in list context, otherwise just | ||
| 273 | C<$port_instance>. | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | sub get_port { | ||
| 278 | my ($device, $portname) = @_; | ||
| 279 | |||
| 280 | # accept either ip or dbic object | ||
| 281 | $device = get_device($device); | ||
| 282 | |||
| 283 | return unless $device and $device->in_storage; | ||
| 284 | |||
| 285 | my $port = schema(vars->{'tenant'})->resultset('DevicePort')->with_properties | ||
| 286 | ->find({ip => $device->ip, port => $portname}); | ||
| 287 | |||
| 288 | return unless $port and $port->in_storage; | ||
| 289 | |||
| 290 | return ( wantarray ? ($device, $port) : $port ); | ||
| 291 | } | ||
| 292 | |||
| 293 | =head2 get_iid( $info, $port ) | ||
| 294 | |||
| 295 | Given an L<SNMP::Info> instance for a device, and the name of a port, returns | ||
| 296 | the current interface table index for that port. This can be used in further | ||
| 297 | SNMP requests on attributes of the port. | ||
| 298 | |||
| 299 | Returns C<undef> if there is no such port name on the device. | ||
| 300 | |||
| 301 | =cut | ||
| 302 | |||
| 303 | sub get_iid { | ||
| 304 | my ($info, $port) = @_; | ||
| 305 | |||
| 306 | # accept either port name or dbic object | ||
| 307 | $port = $port->port if ref $port; | ||
| 308 | |||
| 309 | my $interfaces = $info->interfaces; | ||
| 310 | my %rev_if = reverse %$interfaces; | ||
| 311 | my $iid = $rev_if{$port}; | ||
| 312 | |||
| 313 | return $iid; | ||
| 314 | } | ||
| 315 | |||
| 316 | =head2 get_powerid( $info, $port ) | ||
| 317 | |||
| 318 | Given an L<SNMP::Info> instance for a device, and the name of a port, returns | ||
| 319 | the current PoE table index for the port. This can be used in further SNMP | ||
| 320 | requests on PoE attributes of the port. | ||
| 321 | |||
| 322 | Returns C<undef> if there is no such port name on the device. | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | sub get_powerid { | ||
| 327 | my ($info, $port) = @_; | ||
| 328 | |||
| 329 | # accept either port name or dbic object | ||
| 330 | $port = $port->port if ref $port; | ||
| 331 | |||
| 332 | my $iid = get_iid($info, $port) | ||
| 333 | or return undef; | ||
| 334 | |||
| 335 | my $p_interfaces = $info->peth_port_ifindex; | ||
| 336 | my %rev_p_if = reverse %$p_interfaces; | ||
| 337 | my $powerid = $rev_p_if{$iid}; | ||
| 338 | |||
| 339 | return $powerid; | ||
| 340 | } | ||
| 341 | |||
| 342 | =head2 is_vlan_subinterface( $port ) | ||
| 343 | |||
| 344 | Returns true if the C<$port> L<DBIx::Class> object represents a vlan | ||
| 345 | subinterface or is the logical parent of such a port. | ||
| 346 | |||
| 347 | This uses simple checks on the port I<type> and I<descr>, and therefore might | ||
| 348 | sometimes returns a false-negative result. | ||
| 349 | |||
| 350 | =cut | ||
| 351 | |||
| 352 | sub is_vlan_subinterface { | ||
| 353 | my $port = shift; | ||
| 354 | return true if $port->has_subinterfaces; | ||
| 355 | |||
| 356 | my $is_vlan = (($port->type and | ||
| 357 | $port->type =~ /^(53|propVirtual|l2vlan|l3ipvlan|135|136|137)$/i) | ||
| 358 | or ($port->port and $port->port =~ /vlan/i) | ||
| 359 | or ($port->descr and $port->descr =~ /vlan/i)) ? 1 : 0; | ||
| 360 | |||
| 361 | return $is_vlan; | ||
| 362 | } | ||
| 363 | |||
| 364 | =head2 port_has_phone( $port ) | ||
| 365 | |||
| 366 | Returns true if the C<$port> L<DBIx::Class> object has a phone connected. | ||
| 367 | |||
| 368 | =cut | ||
| 369 | |||
| 370 | sub port_has_phone { | ||
| 371 | my $row = shift; | ||
| 372 | return $row->remote_is_phone if $row->can('remote_is_phone'); | ||
| 373 | my $properties = $row->properties; | ||
| 374 | return ($properties ? $properties->remote_is_phone : undef); | ||
| 375 | } | ||
| 376 | |||
| 377 | =head2 port_has_wap( $port ) | ||
| 378 | |||
| 379 | Returns true if the C<$port> L<DBIx::Class> object has a wireless AP connected. | ||
| 380 | |||
| 381 | =cut | ||
| 382 | |||
| 383 | sub port_has_wap { | ||
| 384 | my $row = shift; | ||
| 385 | return $row->remote_is_wap if $row->can('remote_is_wap'); | ||
| 386 | my $properties = $row->properties; | ||
| 387 | return ($properties ? $properties->remote_is_wap : undef); | ||
| 388 | } | ||
| 389 | |||
| 390 | # copied from SNMP::Info to avoid introducing dependency to web frontend | ||
| 391 | sub munge_highspeed { | ||
| 392 | my $speed = shift; | ||
| 393 | my $fmt = "%d Mbps"; | ||
| 394 | |||
| 395 | if ( $speed > 9999999 ) { | ||
| 396 | $fmt = "%d Tbps"; | ||
| 397 | $speed /= 1000000; | ||
| 398 | } | ||
| 399 | elsif ( $speed > 999999 ) { | ||
| 400 | $fmt = "%.1f Tbps"; | ||
| 401 | $speed /= 1000000.0; | ||
| 402 | } | ||
| 403 | elsif ( $speed > 9999 ) { | ||
| 404 | $fmt = "%d Gbps"; | ||
| 405 | $speed /= 1000; | ||
| 406 | } | ||
| 407 | elsif ( $speed > 999 ) { | ||
| 408 | $fmt = "%.1f Gbps"; | ||
| 409 | $speed /= 1000.0; | ||
| 410 | } | ||
| 411 | return sprintf( $fmt, $speed ); | ||
| 412 | } | ||
| 413 | |||
| 414 | =head2 to_speed( $speed ) | ||
| 415 | |||
| 416 | Incorporate SNMP::Info C<munge_highspeed> to avoid extra dependency on web frontend. | ||
| 417 | |||
| 418 | =cut | ||
| 419 | |||
| 420 | sub to_speed { | ||
| 421 | my $speed = shift or return ''; | ||
| 422 | return $speed if $speed =~ m/\D/; | ||
| 423 | ($speed = munge_highspeed($speed / 1_000_000)) =~ s/\.0 ?//g; | ||
| 424 | return $speed; | ||
| 425 | } | ||
| 426 | |||
| 427 | 1; |