| 1 | package App::Netdisco::Util::Permission; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Dancer qw/:syntax :script/; | ||
| 6 | |||
| 7 | use Scalar::Util qw/blessed reftype/; | ||
| 8 | use NetAddr::IP::Lite ':lower'; | ||
| 9 | use Algorithm::Cron; | ||
| 10 | |||
| 11 | use App::Netdisco::Util::DNS 'hostname_from_ip'; | ||
| 12 | |||
| 13 | use base 'Exporter'; | ||
| 14 | our @EXPORT = (); | ||
| 15 | our @EXPORT_OK = qw/ | ||
| 16 | check_acl | ||
| 17 | check_acl_no | ||
| 18 | check_acl_only | ||
| 19 | acl_matches | ||
| 20 | acl_matches_only | ||
| 21 | permitted_by_acl | ||
| 22 | /; | ||
| 23 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | App::Netdisco::Util::Permission | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | Helper subroutines to support parts of the Netdisco application. | ||
| 32 | |||
| 33 | There are no default exports, however the C<:all> tag will export all | ||
| 34 | subroutines. | ||
| 35 | |||
| 36 | =head1 EXPORT_OK | ||
| 37 | |||
| 38 | =head2 acl_matches( $ip | $object | \%hash | \@item_list, $setting_name | $acl_entry | \@acl ) | ||
| 39 | |||
| 40 | Given an IP address, object instance, or hash, returns true if the | ||
| 41 | configuration setting C<$setting_name> matches, else returns false. | ||
| 42 | |||
| 43 | Usage of this function is strongly advised to be of the form: | ||
| 44 | |||
| 45 | QUIT/SKIP IF acl_matches | ||
| 46 | |||
| 47 | The function fails safe, so if the content of the setting or ACL is undefined | ||
| 48 | or an empty string, then C<acl_matches> also returns true. | ||
| 49 | |||
| 50 | If C<$setting_name> is a valid setting, then it will be resolved to the access | ||
| 51 | control list, else we assume you passed an ACL entry or ACL. | ||
| 52 | |||
| 53 | See L<the Netdisco wiki|https://github.com/netdisco/netdisco/wiki/Configuration#access-control-lists> | ||
| 54 | for details of what C<$acl> may contain. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub acl_matches { | ||
| 59 | my ($thing, $setting_name) = @_; | ||
| 60 | # fail-safe so undef config should return true | ||
| 61 | return true unless $thing and $setting_name; | ||
| 62 | my $config = (exists config->{"$setting_name"} ? setting($setting_name) | ||
| 63 | : $setting_name); | ||
| 64 | return check_acl($thing, $config); | ||
| 65 | } | ||
| 66 | |||
| 67 | =head2 check_acl_no( $ip | $object | \%hash | \@item_list, $setting_name | $acl_entry | \@acl ) | ||
| 68 | |||
| 69 | This is an alias for L<acl_matches>. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub check_acl_no { goto &acl_matches } | ||
| 74 | |||
| 75 | =head2 acl_matches_only( $ip | $object | \%hash | \@item_list, $setting_name | $acl_entry | \@acl ) | ||
| 76 | |||
| 77 | Given an IP address, object instance, or hash, returns true if the | ||
| 78 | configuration setting C<$setting_name> matches, else returns false. | ||
| 79 | |||
| 80 | Usage of this function is strongly advised to be of the form: | ||
| 81 | |||
| 82 | QUIT/SKIP UNLESS acl_matches_only | ||
| 83 | |||
| 84 | The function fails safe, so if the content of the setting or ACL is undefined | ||
| 85 | or an empty string, then C<acl_matches_only> also returns false. | ||
| 86 | |||
| 87 | Further, if the setting or ACL resolves to a list but the list has no items, | ||
| 88 | then C<acl_matches_only> returns true (as if there is a successful match). | ||
| 89 | |||
| 90 | If C<$setting_name> is a valid setting, then it will be resolved to the access | ||
| 91 | control list, else we assume you passed an ACL entry or ACL. | ||
| 92 | |||
| 93 | See L<the Netdisco wiki|https://github.com/netdisco/netdisco/wiki/Configuration#access-control-lists> | ||
| 94 | for details of what C<$acl> may contain. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub acl_matches_only { | ||
| 99 | my ($thing, $setting_name) = @_; | ||
| 100 | # fail-safe so undef config should return false | ||
| 101 | return false unless $thing and $setting_name; | ||
| 102 | my $config = (exists config->{"$setting_name"} ? setting($setting_name) | ||
| 103 | : $setting_name); | ||
| 104 | # logic to make an empty config be equivalent to 'any' (i.e. a match) | ||
| 105 | # empty list check means truth check passes for match or empty list | ||
| 106 | return true if not $config # undef or empty string | ||
| 107 | or ((ref [] eq ref $config) and not scalar @$config); | ||
| 108 | return check_acl($thing, $config); | ||
| 109 | } | ||
| 110 | |||
| 111 | =head2 check_acl_only( $ip | $object | \%hash | \@item_list, $setting_name | $acl_entry | \@acl ) | ||
| 112 | |||
| 113 | This is an alias for L<acl_matches_only>. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | sub check_acl_only { goto &acl_matches_only } | ||
| 118 | |||
| 119 | =head2 permitted_by_acl( $thing, \%config ) | ||
| 120 | |||
| 121 | Given an object and a config hash carrying optional C<no> and C<only> ACLs | ||
| 122 | (as custom fields, extra device port columns and external links do), returns | ||
| 123 | true if C<$thing> is not matched by C<no> and is matched by C<only>. Returns | ||
| 124 | false when either argument is missing. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | # Also the permitted_by_acl template token in Web.pm, which wraps this: the | ||
| 129 | # Ports tab builds its JSON rows in Perl and has to hide the same cells the | ||
| 130 | # templates hide. | ||
| 131 | sub permitted_by_acl { | ||
| 132 | my ($thing, $config) = @_; | ||
| 133 | return false unless $thing and $config; | ||
| 134 | |||
| 135 | return if acl_matches($thing, ($config->{no} || [])); | ||
| 136 | return unless acl_matches_only($thing, ($config->{only} || [])); | ||
| 137 | return true; | ||
| 138 | } | ||
| 139 | |||
| 140 | =head2 check_acl( $ip | $object | \%hash | \@item_list, $acl_entry | \@acl ) | ||
| 141 | |||
| 142 | Given an IP address, object instance, or hash, compares it to the items in | ||
| 143 | C<< \@acl >> then returns true or false. You can control whether any item must | ||
| 144 | match or all must match, and items can be negated to invert the match logic. | ||
| 145 | |||
| 146 | Also accepts an array reference of multiple IP addresses, object instances, | ||
| 147 | and hashes, and will test against each in turn, for each ACL rule. | ||
| 148 | |||
| 149 | The slots C<alias>, C<ip>, C<switch>, and C<addr> are looked for in the | ||
| 150 | instance or hash and used to compare a bare IP address (so it works with most | ||
| 151 | Netdisco database classes, and the L<NetAddr::IP> class). Any instance or hash | ||
| 152 | slot can be used as an ACL named property. | ||
| 153 | |||
| 154 | There are several options for what C<< \@acl >> may contain. See | ||
| 155 | L<the Netdisco wiki|https://github.com/netdisco/netdisco/wiki/Configuration#access-control-lists> | ||
| 156 | for the details. | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | # groups being checked further up this call, so a group that refers back to | ||
| 161 | # itself, directly or through others, matches nothing where it recurs | ||
| 162 | # instead of recursing without end. ACL Manager host ACLs are named groups, | ||
| 163 | # so such a loop is one rule away. | ||
| 164 | our %IN_GROUP; | ||
| 165 | |||
| 166 | sub check_acl { | ||
| 167 | my ($things, $config) = @_; | ||
| 168 | return false unless defined $things and defined $config; | ||
| 169 | return false if ref [] eq ref $things and not scalar @$things; | ||
| 170 | $things = [$things] if ref [] ne ref $things; | ||
| 171 | |||
| 172 | my $real_ip = ''; # valid to be empty | ||
| 173 | ITEM: foreach my $item (@$things) { | ||
| 174 | foreach my $slot (qw/alias ip switch addr/) { | ||
| 175 | if (blessed $item) { | ||
| 176 | $real_ip = $item->$slot if $item->can($slot) | ||
| 177 | and eval { $item->$slot }; | ||
| 178 | } | ||
| 179 | elsif (ref {} eq ref $item) { | ||
| 180 | $real_ip = $item->{$slot} if exists $item->{$slot} | ||
| 181 | and $item->{$slot}; | ||
| 182 | } | ||
| 183 | last ITEM if $real_ip; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | ITEM: foreach my $item (@$things) { | ||
| 187 | last ITEM if $real_ip; | ||
| 188 | $real_ip = $item if (ref $item eq q{}) and $item; | ||
| 189 | } | ||
| 190 | |||
| 191 | $config = [$config] if ref $config eq q{}; | ||
| 192 | if (ref [] ne ref $config) { | ||
| 193 | error "error: acl is not a single item or list (cannot compare to '$real_ip')"; | ||
| 194 | return false; | ||
| 195 | } | ||
| 196 | my $all = (scalar grep {$_ eq 'op:and'} @$config); | ||
| 197 | |||
| 198 | # common case of using plain IP in ACL, so string compare for speed | ||
| 199 | my $find = (scalar grep {not reftype $_ and $_ eq $real_ip} @$config); | ||
| 200 | return true if $real_ip and $find and not $all; | ||
| 201 | |||
| 202 | my $addr = NetAddr::IP::Lite->new($real_ip); | ||
| 203 | my $name = undef; # only look up once, and only if qr// is used | ||
| 204 | my $ropt = { retry => 1, retrans => 1, udp_timeout => 1, tcp_timeout => 2 }; | ||
| 205 | my $qref = ref qr//; | ||
| 206 | |||
| 207 | RULE: foreach (@$config) { | ||
| 208 | my $rule = $_; # must copy so that we can modify safely | ||
| 209 | next RULE if !defined $rule or $rule eq 'op:and'; | ||
| 210 | |||
| 211 | if ($qref eq ref $rule) { | ||
| 212 | # if no IP addr, cannot match its dns | ||
| 213 | next RULE unless $addr; | ||
| 214 | |||
| 215 | $name = ($name || hostname_from_ip($addr->addr, $ropt) || '!!none!!'); | ||
| 216 | if ($name =~ $rule) { | ||
| 217 | return true if not $all; | ||
| 218 | } | ||
| 219 | else { | ||
| 220 | return false if $all; | ||
| 221 | } | ||
| 222 | next RULE; | ||
| 223 | } | ||
| 224 | |||
| 225 | my $neg = ($rule =~ s/^!//); | ||
| 226 | |||
| 227 | if ($rule =~ m/^group:(.+)$/) { | ||
| 228 | my $group = $1; | ||
| 229 | # an unknown group matches nothing; reading must not write into config | ||
| 230 | my $rules = (setting('host_groups') || {})->{$group} || []; | ||
| 231 | |||
| 232 | my $match = false; | ||
| 233 | if (not $IN_GROUP{$group}) { | ||
| 234 | local $IN_GROUP{$group} = 1; | ||
| 235 | $match = check_acl($things, $rules); | ||
| 236 | } | ||
| 237 | |||
| 238 | if ($neg xor $match) { | ||
| 239 | return true if not $all; | ||
| 240 | } | ||
| 241 | else { | ||
| 242 | return false if $all; | ||
| 243 | } | ||
| 244 | next RULE; | ||
| 245 | } | ||
| 246 | |||
| 247 | if ($rule =~ m/^tag:(.+)$/) { | ||
| 248 | my $tag = $1; | ||
| 249 | my $found = false; | ||
| 250 | |||
| 251 | ITEM: foreach my $item (@$things) { | ||
| 252 | if (blessed $item and $item->can('tags')) { | ||
| 253 | if ($neg xor scalar grep {$_ eq $tag} @{ $item->tags || [] }) { | ||
| 254 | return true if not $all; | ||
| 255 | $found = true; | ||
| 256 | last ITEM; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | elsif (ref {} eq ref $item and exists $item->{'tags'}) { | ||
| 260 | if ($neg xor scalar grep {$_ eq $tag} @{ $item->{'tags'} || [] }) { | ||
| 261 | return true if not $all; | ||
| 262 | $found = true; | ||
| 263 | last ITEM; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | return false if $all and not $found; | ||
| 269 | next RULE; | ||
| 270 | } | ||
| 271 | |||
| 272 | # cf:customfield:val | ||
| 273 | if ($rule =~ m/^cf:([^:]+):(.*)$/) { | ||
| 274 | my $prop = $1; | ||
| 275 | my $match = $2 || ''; | ||
| 276 | my $found = false; | ||
| 277 | |||
| 278 | # custom field exists, undef is allowed to match empty string | ||
| 279 | ITEM: foreach my $item (@$things) { | ||
| 280 | my $cf = {}; | ||
| 281 | if (blessed $item and $item->can('custom_fields')) { | ||
| 282 | $cf = from_json ($item->custom_fields || '{}'); | ||
| 283 | } | ||
| 284 | elsif (ref {} eq ref $item and exists $item->{'custom_fields'}) { | ||
| 285 | $cf = from_json ($item->{'custom_fields'} || '{}'); | ||
| 286 | } | ||
| 287 | |||
| 288 | if (ref {} eq ref $cf and exists $cf->{$prop}) { | ||
| 289 | if ($neg xor | ||
| 290 | ((!defined $cf->{$prop} and $match eq q{}) | ||
| 291 | or | ||
| 292 | (defined $cf->{$prop} and ref $cf->{$prop} eq q{} and $cf->{$prop} =~ m/^$match$/)) ) { | ||
| 293 | return true if not $all; | ||
| 294 | $found = true; | ||
| 295 | last ITEM; | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | # missing custom field matches empty string | ||
| 301 | # #1348 or matches string if $neg is set | ||
| 302 | # (which is done in a second pass to allow all @$things to be | ||
| 303 | # inspected for existing custom fields) | ||
| 304 | if (! $found and ($match eq q{} and not $neg) or (length $match and $neg)) { | ||
| 305 | |||
| 306 | ITEM: foreach my $item (@$things) { | ||
| 307 | my $cf = {}; | ||
| 308 | if (blessed $item and $item->can('custom_fields')) { | ||
| 309 | $cf = from_json ($item->custom_fields || '{}'); | ||
| 310 | } | ||
| 311 | elsif (ref {} eq ref $item and exists $item->{'custom_fields'}) { | ||
| 312 | $cf = from_json ($item->{'custom_fields'} || '{}'); | ||
| 313 | } | ||
| 314 | |||
| 315 | # empty or missing property | ||
| 316 | if (ref {} eq ref $cf and ! exists $cf->{$prop}) { | ||
| 317 | return true if not $all; | ||
| 318 | $found = true; | ||
| 319 | last ITEM; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | } | ||
| 323 | |||
| 324 | return false if $all and not $found; | ||
| 325 | next RULE; | ||
| 326 | } | ||
| 327 | |||
| 328 | # prop:val | ||
| 329 | # with a check that prop isn't just the first part of a v6 addr | ||
| 330 | if ($rule =~ m/^([^:]+):(.*)$/ and $1 !~ m/^[a-f0-9]+$/i) { | ||
| 331 | my $prop = $1; | ||
| 332 | my $match = $2 || ''; | ||
| 333 | my $found = false; | ||
| 334 | |||
| 335 | # property exists, undef is allowed to match empty string | ||
| 336 | ITEM: foreach my $item (@$things) { | ||
| 337 | if (blessed $item and $item->can($prop)) { | ||
| 338 | if ($neg xor | ||
| 339 | ((!defined eval { $item->$prop } and $match eq q{}) | ||
| 340 | or | ||
| 341 | (defined eval { $item->$prop } and ref $item->$prop eq q{} and $item->$prop =~ m/^$match$/)) ) { | ||
| 342 | return true if not $all; | ||
| 343 | $found = true; | ||
| 344 | last ITEM; | ||
| 345 | } | ||
| 346 | } | ||
| 347 | elsif (ref {} eq ref $item and exists $item->{$prop}) { | ||
| 348 | if ($neg xor | ||
| 349 | ((!defined $item->{$prop} and $match eq q{}) | ||
| 350 | or | ||
| 351 | (defined $item->{$prop} and ref $item->{$prop} eq q{} and $item->{$prop} =~ m/^$match$/)) ) { | ||
| 352 | return true if not $all; | ||
| 353 | $found = true; | ||
| 354 | last ITEM; | ||
| 355 | } | ||
| 356 | } | ||
| 357 | } | ||
| 358 | |||
| 359 | # missing property matches empty string | ||
| 360 | # #1348 or matches string if $neg is set | ||
| 361 | # (which is done in a second pass to allow all @$things to be | ||
| 362 | # inspected for existing properties) | ||
| 363 | if (! $found and ($match eq q{} and not $neg) or (length $match and $neg)) { | ||
| 364 | |||
| 365 | ITEM: foreach my $item (@$things) { | ||
| 366 | if (blessed $item and ! $item->can($prop)) { | ||
| 367 | return true if not $all; | ||
| 368 | $found = true; | ||
| 369 | last ITEM; | ||
| 370 | } | ||
| 371 | elsif (ref {} eq ref $item and ! exists $item->{$prop}) { | ||
| 372 | return true if not $all; | ||
| 373 | $found = true; | ||
| 374 | last ITEM; | ||
| 375 | } | ||
| 376 | } | ||
| 377 | } | ||
| 378 | |||
| 379 | return false if $all and not $found; | ||
| 380 | next RULE; | ||
| 381 | } | ||
| 382 | |||
| 383 | if ($rule =~ m/^\S+\s+\S+\s+\S+\s+\S+\s+\S+/i) { | ||
| 384 | my $win_start = time - (time % 60) - 1; | ||
| 385 | my $win_end = $win_start + 60; | ||
| 386 | my $cron = Algorithm::Cron->new( | ||
| 387 | base => 'local', | ||
| 388 | crontab => $rule, | ||
| 389 | ) or next RULE; | ||
| 390 | |||
| 391 | if ($neg xor ($cron->next_time($win_start) <= $win_end)) { | ||
| 392 | return true if not $all; | ||
| 393 | } | ||
| 394 | else { | ||
| 395 | return false if $all; | ||
| 396 | } | ||
| 397 | next RULE; | ||
| 398 | } | ||
| 399 | |||
| 400 | if ($rule =~ m/[:.]([a-f0-9]+)-([a-f0-9]+)$/i) { | ||
| 401 | my $first = $1; | ||
| 402 | my $last = $2; | ||
| 403 | |||
| 404 | # if no IP addr, cannot match IP range | ||
| 405 | next RULE unless $addr; | ||
| 406 | |||
| 407 | if ($rule =~ m/:/) { | ||
| 408 | next RULE if $addr->bits != 128 and not $all; | ||
| 409 | |||
| 410 | $first = hex $first; | ||
| 411 | $last = hex $last; | ||
| 412 | |||
| 413 | (my $header = $rule) =~ s/:[^:]+$/:/; | ||
| 414 | foreach my $part ($first .. $last) { | ||
| 415 | my $ip = NetAddr::IP::Lite->new($header . sprintf('%x',$part) . '/128') | ||
| 416 | or next; | ||
| 417 | if ($neg xor ($ip == $addr)) { | ||
| 418 | return true if not $all; | ||
| 419 | next RULE; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | return false if (not $neg and $all); | ||
| 423 | return true if ($neg and not $all); | ||
| 424 | } | ||
| 425 | else { | ||
| 426 | next RULE if $addr->bits != 32 and not $all; | ||
| 427 | |||
| 428 | (my $header = $rule) =~ s/\.[^.]+$/./; | ||
| 429 | foreach my $part ($first .. $last) { | ||
| 430 | my $ip = NetAddr::IP::Lite->new($header . $part . '/32') | ||
| 431 | or next; | ||
| 432 | if ($neg xor ($ip == $addr)) { | ||
| 433 | return true if not $all; | ||
| 434 | next RULE; | ||
| 435 | } | ||
| 436 | } | ||
| 437 | return false if (not $neg and $all); | ||
| 438 | return true if ($neg and not $all); | ||
| 439 | } | ||
| 440 | next RULE; | ||
| 441 | } | ||
| 442 | |||
| 443 | # could be something in error, and IP/host is only option left | ||
| 444 | next RULE if ref $rule; | ||
| 445 | |||
| 446 | # if no IP addr, cannot match IP prefix | ||
| 447 | next RULE unless $addr; | ||
| 448 | |||
| 449 | my $ip = NetAddr::IP::Lite->new($rule) | ||
| 450 | or next RULE; | ||
| 451 | next RULE if $ip->bits != $addr->bits and not $all; | ||
| 452 | |||
| 453 | if ($neg xor ($ip->contains($addr))) { | ||
| 454 | return true if not $all; | ||
| 455 | } | ||
| 456 | else { | ||
| 457 | return false if $all; | ||
| 458 | } | ||
| 459 | |||
| 460 | next RULE; | ||
| 461 | } | ||
| 462 | |||
| 463 | return ($all ? true : false); | ||
| 464 | } | ||
| 465 | |||
| 466 | true; |