« blib/lib/App/Netdisco/Worker/Loader.pm
↑ Summary
blib/lib/App/Netdisco/Web/Plugin/Device/Neighbors.pm »
| 1 | package App::Netdisco::Web::Auth::Provider::DBIC; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Dancer::Plugin::Auth::Extensible::Provider::Base'; | ||
| 7 | |||
| 8 | # with thanks to yanick's patch at | ||
| 9 | # https://github.com/bigpresh/Dancer-Plugin-Auth-Extensible/pull/24 | ||
| 10 | |||
| 11 | use Dancer ':syntax'; | ||
| 12 | use Dancer::Plugin::DBIC; | ||
| 13 | use Dancer::Plugin::Passphrase; | ||
| 14 | use Digest::MD5; | ||
| 15 | use Net::LDAP; | ||
| 16 | use Authen::Radius; | ||
| 17 | use Path::Class; | ||
| 18 | use File::ShareDir 'dist_dir'; | ||
| 19 | use Try::Tiny; | ||
| 20 | use NetAddr::IP::Lite ':lower'; | ||
| 21 | |||
| 22 | use App::Netdisco::Util::Permission 'check_acl'; | ||
| 23 | use App::Netdisco::Util::Web | ||
| 24 | qw/trusted_client_address warn_forwarded_address_ignored | ||
| 25 | tacacs_configured tacacs_unavailable/; | ||
| 26 | |||
| 27 | sub authenticate_user { | ||
| 28 | my ($self, $username, $password) = @_; | ||
| 29 | return unless defined $username; | ||
| 30 | |||
| 31 | my $user = $self->get_user_details($username) or return; | ||
| 32 | return unless $user->in_storage; | ||
| 33 | return $self->match_password($password, $user); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub get_user_details { | ||
| 37 | my ($self, $username) = @_; | ||
| 38 | |||
| 39 | my $settings = $self->realm_settings; | ||
| 40 | my $database = schema($settings->{schema_name}) | ||
| 41 | or die "No database connection"; | ||
| 42 | |||
| 43 | my $users_table = $settings->{users_resultset} || 'User'; | ||
| 44 | my $username_column = $settings->{users_username_column} || 'username'; | ||
| 45 | |||
| 46 | my $user = try { | ||
| 47 | $database->resultset($users_table)->find({ | ||
| 48 | # case insensitive match on username, no wildcards | ||
| 49 | $username_column => { -ilike => quotemeta($username) }, | ||
| 50 | }); | ||
| 51 | }; | ||
| 52 | |||
| 53 | # each of these settings permits no user in the database | ||
| 54 | # so create a pseudo user entry instead | ||
| 55 | if (not $user and | ||
| 56 | (setting('no_auth') or | ||
| 57 | (not setting('validate_remote_user') | ||
| 58 | and (setting('trust_remote_user') or setting('trust_x_remote_user')) ))) { | ||
| 59 | |||
| 60 | $user = $database->resultset($users_table) | ||
| 61 | ->new_result({username => $username}); | ||
| 62 | } | ||
| 63 | |||
| 64 | return $user; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub validate_api_token { | ||
| 68 | my ($self, $token) = @_; | ||
| 69 | return unless defined $token; | ||
| 70 | |||
| 71 | my $settings = $self->realm_settings; | ||
| 72 | my $database = schema($settings->{schema_name}) | ||
| 73 | or die "No database connection"; | ||
| 74 | |||
| 75 | my $users_table = $settings->{users_resultset} || 'User'; | ||
| 76 | my $token_column = $settings->{users_token_column} || 'token'; | ||
| 77 | |||
| 78 | $token =~ s/^(?:Apikey|Bearer) //i; # swagger-ui omits scheme; also accept Bearer | ||
| 79 | my $user = try { | ||
| 80 | $database->resultset($users_table)->find({ $token_column => $token }, | ||
| 81 | { prefetch => { token_acl_name => { mappings => [qw/left_acl right_acl/] } } }); | ||
| 82 | }; | ||
| 83 | |||
| 84 | return undef unless $user and $user->in_storage and $user->token_from | ||
| 85 | and ($user->token_no_expire | ||
| 86 | or $user->token_from > (time - setting('api_token_lifetime'))); | ||
| 87 | |||
| 88 | # the ACL is loaded when the user is loaded from the DB above | ||
| 89 | if ($user->token_acl) { | ||
| 90 | # NetAddr::IP::Lite->new(undef) is a truthy 0.0.0.0/0 in every stable | ||
| 91 | # release, which a negated list matches, so refuse an absent address here. | ||
| 92 | my $address = trusted_client_address(); | ||
| 93 | my $client = ($address ? NetAddr::IP::Lite->new($address) : undef); | ||
| 94 | if (not $client | ||
| 95 | or not _token_acl_matches($client, $user->token_acl_name)) { | ||
| 96 | warn_forwarded_address_ignored('an API token access list'); | ||
| 97 | return undef; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | return $user; | ||
| 102 | } | ||
| 103 | |||
| 104 | # Matches the rows fetched with the user, not host_groups[acl name]. Tokens | ||
| 105 | # always belong to the default database, while host_groups holds the current | ||
| 106 | # tenant's named ACLs, so under /t/<tenant>/ a tenant ACL of the same name | ||
| 107 | # must not decide whether a token is accepted. No rows fails closed, as | ||
| 108 | # check_acl on an empty list would not. | ||
| 109 | sub _token_acl_matches { | ||
| 110 | my ($client, $acl) = @_; | ||
| 111 | return 0 unless $acl; | ||
| 112 | foreach my $map ($acl->mappings->all) { | ||
| 113 | return 1 if check_acl($client, $map->left_acl->rules); | ||
| 114 | } | ||
| 115 | return 0; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub get_user_roles { | ||
| 119 | my ($self, $username) = @_; | ||
| 120 | return unless defined $username; | ||
| 121 | |||
| 122 | my $settings = $self->realm_settings; | ||
| 123 | my $database = schema($settings->{schema_name}) | ||
| 124 | or die "No database connection"; | ||
| 125 | |||
| 126 | # Get details of the user first; both to check they exist, and so we have | ||
| 127 | # their ID to use. | ||
| 128 | my $user = $self->get_user_details($username) | ||
| 129 | or return; | ||
| 130 | |||
| 131 | my $roles = $settings->{roles_relationship} || 'roles'; | ||
| 132 | my $role_column = $settings->{role_column} || 'role'; | ||
| 133 | |||
| 134 | # this method returns a list of current user roles | ||
| 135 | # but for API with trust_remote_user, trust_x_remote_user, and no_auth | ||
| 136 | # we need to fake that there is a valid API key | ||
| 137 | |||
| 138 | my $api_requires_key = | ||
| 139 | (setting('trust_remote_user') or setting('trust_x_remote_user') or setting('no_auth')) | ||
| 140 | eq '1' ? 'false' : 'true'; | ||
| 141 | |||
| 142 | return [ try { | ||
| 143 | $user->$roles->search({}, { bind => [ | ||
| 144 | $api_requires_key, setting('api_token_lifetime'), | ||
| 145 | $api_requires_key, setting('api_token_lifetime'), | ||
| 146 | ] })->get_column( $role_column )->all; | ||
| 147 | } ]; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub match_password { | ||
| 151 | my($self, $password, $user) = @_; | ||
| 152 | return unless $user; | ||
| 153 | |||
| 154 | my $settings = $self->realm_settings; | ||
| 155 | my $username_column = $settings->{users_username_column} || 'username'; | ||
| 156 | |||
| 157 | my $pwmatch_result = 0; | ||
| 158 | my $username = $user->$username_column; | ||
| 159 | |||
| 160 | if ($user->ldap) { | ||
| 161 | $pwmatch_result = $self->match_with_ldap($password, $username); | ||
| 162 | } | ||
| 163 | elsif ($user->radius) { | ||
| 164 | $pwmatch_result = $self->match_with_radius($password, $username); | ||
| 165 | } | ||
| 166 | elsif ($user->tacacs) { | ||
| 167 | $pwmatch_result = $self->match_with_tacacs($password, $username); | ||
| 168 | } | ||
| 169 | else { | ||
| 170 | $pwmatch_result = $self->match_with_local_pass($password, $user); | ||
| 171 | } | ||
| 172 | |||
| 173 | return $pwmatch_result; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub match_with_local_pass { | ||
| 177 | my($self, $password, $user) = @_; | ||
| 178 | |||
| 179 | my $settings = $self->realm_settings; | ||
| 180 | my $password_column = $settings->{users_password_column} || 'password'; | ||
| 181 | |||
| 182 | return unless $password and $user->$password_column; | ||
| 183 | |||
| 184 | if ($user->$password_column !~ m/^{[A-Z]+}/) { | ||
| 185 | my $sum = Digest::MD5::md5_hex($password); | ||
| 186 | |||
| 187 | if ($sum eq $user->$password_column) { | ||
| 188 | if (setting('safe_password_store')) { | ||
| 189 | # upgrade password if successful, and permitted | ||
| 190 | $user->update({password => passphrase($password)->generate}); | ||
| 191 | } | ||
| 192 | return 1; | ||
| 193 | } | ||
| 194 | else { | ||
| 195 | return 0; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | else { | ||
| 199 | return 0 unless passphrase($password)->matches($user->$password_column); | ||
| 200 | |||
| 201 | # a hash carries the work factor it was made with, so a raised setting | ||
| 202 | # reaches an existing password only by rewriting it on a good login | ||
| 203 | if (setting('safe_password_store') | ||
| 204 | and password_hash_is_stale($user->$password_column)) { | ||
| 205 | $user->update({password => passphrase($password)->generate}); | ||
| 206 | } | ||
| 207 | return 1; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | # True only when the stored hash is bcrypt, the configured work factor is | ||
| 212 | # readable, and the hash was made with a lower one. Anything else is left | ||
| 213 | # alone: an unrecognised scheme is not ours to rewrite. | ||
| 214 | sub password_hash_is_stale { | ||
| 215 | my $hash = shift; | ||
| 216 | return 0 unless defined $hash; | ||
| 217 | |||
| 218 | my ($stored) = $hash =~ m/^\{CRYPT\}\$2a\$(\d\d)\$/; | ||
| 219 | return 0 unless defined $stored; | ||
| 220 | |||
| 221 | my $wanted = ((setting('plugins') || {})->{'Passphrase'} || {}) | ||
| 222 | ->{'Bcrypt'}->{'cost'}; | ||
| 223 | return 0 unless defined $wanted and $wanted =~ m/^\d+$/; | ||
| 224 | |||
| 225 | return (($stored + 0) < ($wanted + 0)) ? 1 : 0; | ||
| 226 | } | ||
| 227 | |||
| 228 | sub match_with_ldap { | ||
| 229 | my($self, $pass, $user) = @_; | ||
| 230 | |||
| 231 | return unless setting('ldap') and ref {} eq ref setting('ldap'); | ||
| 232 | my $conf = setting('ldap'); | ||
| 233 | |||
| 234 | my $ldapuser = $conf->{user_string}; | ||
| 235 | $ldapuser =~ s/\%USER\%?/$user/egi; | ||
| 236 | |||
| 237 | # If we can bind as anonymous or proxy user, | ||
| 238 | # search for user's distinguished name | ||
| 239 | if ($conf->{proxy_user}) { | ||
| 240 | my $user = $conf->{proxy_user}; | ||
| 241 | my $pass = $conf->{proxy_pass}; | ||
| 242 | my $attrs = ['distinguishedName']; | ||
| 243 | my $result = _ldap_search($ldapuser, $attrs, $user, $pass); | ||
| 244 | $ldapuser = $result->[0] if ($result->[0]); | ||
| 245 | } | ||
| 246 | # otherwise, if we can't search and aren't using AD and then construct DN | ||
| 247 | # by appending base | ||
| 248 | elsif ($ldapuser =~ m/=/) { | ||
| 249 | $ldapuser = "$ldapuser,$conf->{base}"; | ||
| 250 | } | ||
| 251 | |||
| 252 | foreach my $server (@{$conf->{servers}}) { | ||
| 253 | my $opts = $conf->{opts} || {}; | ||
| 254 | my $ldap = Net::LDAP->new($server, %$opts) or next; | ||
| 255 | my $msg = undef; | ||
| 256 | |||
| 257 | if ($conf->{tls_opts} ) { | ||
| 258 | $msg = $ldap->start_tls(%{$conf->{tls_opts}}); | ||
| 259 | } | ||
| 260 | |||
| 261 | $msg = $ldap->bind($ldapuser, password => $pass); | ||
| 262 | $ldap->unbind(); # take down session | ||
| 263 | |||
| 264 | return 1 unless $msg->code(); | ||
| 265 | } | ||
| 266 | |||
| 267 | return undef; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub _ldap_search { | ||
| 271 | my ($filter, $attrs, $user, $pass) = @_; | ||
| 272 | my $conf = setting('ldap'); | ||
| 273 | |||
| 274 | return undef unless defined($filter); | ||
| 275 | return undef if (defined $attrs and ref [] ne ref $attrs); | ||
| 276 | |||
| 277 | foreach my $server (@{$conf->{servers}}) { | ||
| 278 | my $opts = $conf->{opts} || {}; | ||
| 279 | my $ldap = Net::LDAP->new($server, %$opts) or next; | ||
| 280 | my $msg = undef; | ||
| 281 | |||
| 282 | if ($conf->{tls_opts}) { | ||
| 283 | $msg = $ldap->start_tls(%{$conf->{tls_opts}}); | ||
| 284 | } | ||
| 285 | |||
| 286 | if ( $user and $user ne 'anonymous' ) { | ||
| 287 | $msg = $ldap->bind($user, password => $pass); | ||
| 288 | } | ||
| 289 | else { | ||
| 290 | $msg = $ldap->bind(); | ||
| 291 | } | ||
| 292 | |||
| 293 | $msg = $ldap->search( | ||
| 294 | base => $conf->{base}, | ||
| 295 | filter => "($filter)", | ||
| 296 | attrs => $attrs, | ||
| 297 | ); | ||
| 298 | |||
| 299 | $ldap->unbind(); # take down session | ||
| 300 | |||
| 301 | my $entries = [$msg->entries]; | ||
| 302 | return $entries unless $msg->code(); | ||
| 303 | } | ||
| 304 | |||
| 305 | return undef; | ||
| 306 | } | ||
| 307 | |||
| 308 | sub match_with_radius { | ||
| 309 | my($self, $pass, $user) = @_; | ||
| 310 | return unless setting('radius') and ref {} eq ref setting('radius'); | ||
| 311 | |||
| 312 | my $conf = setting('radius'); | ||
| 313 | my $servers = (ref [] eq ref $conf->{'server'} | ||
| 314 | ? $conf->{'server'} : [$conf->{'server'}]); | ||
| 315 | my $radius = Authen::Radius->new( | ||
| 316 | NodeList => $servers, | ||
| 317 | Secret => $conf->{'secret'}, | ||
| 318 | TimeOut => $conf->{'timeout'} || 15, | ||
| 319 | ); | ||
| 320 | my $dict_dir = Path::Class::Dir->new( dist_dir('App-Netdisco') ) | ||
| 321 | ->subdir('contrib')->subdir('raddb')->file('dictionary')->stringify; | ||
| 322 | Authen::Radius->load_dictionary($dict_dir); | ||
| 323 | |||
| 324 | $radius->add_attributes( | ||
| 325 | { Name => 'User-Name', Value => $user }, | ||
| 326 | { Name => 'User-Password', Value => $pass } | ||
| 327 | ); | ||
| 328 | |||
| 329 | if ($conf->{'vsa'}) { | ||
| 330 | foreach my $vsa (@{$conf->{'vsa'}}) { | ||
| 331 | $radius->add_attributes( | ||
| 332 | { | ||
| 333 | Name => $vsa->{'name'}, | ||
| 334 | Value => $vsa->{'value'}, | ||
| 335 | Type => $vsa->{'type'}, | ||
| 336 | Vendor => $vsa->{'vendor'}, | ||
| 337 | Tag => $vsa->{'tag'} | ||
| 338 | }, | ||
| 339 | ); | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | |||
| 344 | $radius->send_packet(ACCESS_REQUEST); | ||
| 345 | |||
| 346 | my $type = $radius->recv_packet(); | ||
| 347 | my $radius_return = ($type eq ACCESS_ACCEPT) ? 1 : 0; | ||
| 348 | |||
| 349 | return $radius_return; | ||
| 350 | } | ||
| 351 | |||
| 352 | sub match_with_tacacs { | ||
| 353 | my($self, $pass, $user) = @_; | ||
| 354 | return unless tacacs_configured(); | ||
| 355 | |||
| 356 | if (my $advice = tacacs_unavailable()) { | ||
| 357 | error sprintf 'TACACS+ login for %s refused: %s', $user, $advice; | ||
| 358 | return undef; | ||
| 359 | } | ||
| 360 | |||
| 361 | my $conf = setting('tacacs'); | ||
| 362 | my $tacacs = new Authen::TacacsPlus(@$conf); | ||
| 363 | if (not $tacacs) { | ||
| 364 | debug sprintf('auth error: Authen::TacacsPlus: %s', Authen::TacacsPlus::errmsg()); | ||
| 365 | return undef; | ||
| 366 | } | ||
| 367 | |||
| 368 | my $tacacs_return = $tacacs->authen($user,$pass); | ||
| 369 | if (not $tacacs_return) { | ||
| 370 | debug sprintf('error: Authen::TacacsPlus: %s', Authen::TacacsPlus::errmsg()); | ||
| 371 | } | ||
| 372 | $tacacs->close(); | ||
| 373 | |||
| 374 | return $tacacs_return; | ||
| 375 | } | ||
| 376 | |||
| 377 | 1; |