|
| 1 | +// Copyright 2019 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +//! Unix domain socket (UDS) peer credentials authenticator |
| 4 | +//! |
| 5 | +//! The `UDSAuthenticator` uses peer credentials from Unix domain sockets to perform |
| 6 | +//! authentication. As such, it uses the UID/GID to grab a username for the owner of the connecting |
| 7 | +//! process. This is used as the application name. |
| 8 | +
|
| 9 | +use super::ApplicationName; |
| 10 | +use super::Authenticate; |
| 11 | +use crate::front::listener::ConnectionMetadata; |
| 12 | +use log::error; |
| 13 | +use parsec_interface::requests::request::RequestAuth; |
| 14 | +use parsec_interface::requests::{ResponseStatus, Result}; |
| 15 | +use users::get_user_by_uid; |
| 16 | + |
| 17 | +#[derive(Copy, Clone, Debug)] |
| 18 | +pub struct UDSAuthenticator; |
| 19 | + |
| 20 | +impl Authenticate for UDSAuthenticator { |
| 21 | + fn authenticate( |
| 22 | + &self, |
| 23 | + _auth: &RequestAuth, |
| 24 | + meta: Option<ConnectionMetadata>, |
| 25 | + ) -> Result<ApplicationName> { |
| 26 | + let meta = match meta { |
| 27 | + Some(meta) => meta, |
| 28 | + None => { |
| 29 | + error!("Authenticator did not receive any metadata; cannot authenticate."); |
| 30 | + return Err(ResponseStatus::AuthenticationError); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + let ucred = match meta { |
| 35 | + ConnectionMetadata::PeerCredentials(ucred) => ucred, |
| 36 | + // TODO: add wildcard pattern when `ConnectionMetadata` has more possibilities. |
| 37 | + }; |
| 38 | + |
| 39 | + let user = match get_user_by_uid(ucred.uid) { |
| 40 | + Some(user) => user, |
| 41 | + None => { |
| 42 | + error!("Couldn't get username for UID!"); |
| 43 | + return Err(ResponseStatus::AuthenticationError); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + let username = match user.name().to_str() { |
| 48 | + Some(username) => username, |
| 49 | + None => { |
| 50 | + error!("Couldn't get username for user!"); |
| 51 | + return Err(ResponseStatus::AuthenticationError); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + Ok(ApplicationName(String::from(username))) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod test { |
| 61 | + use super::super::Authenticate; |
| 62 | + use super::UDSAuthenticator; |
| 63 | + use crate::front::listener::ConnectionMetadata; |
| 64 | + use parsec_interface::requests::request::RequestAuth; |
| 65 | + use parsec_interface::requests::ResponseStatus; |
| 66 | + use std::os::unix::net::UCred; |
| 67 | + use std::os::unix::net::UnixStream; |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn successful_authentication() { |
| 71 | + // This test should PASS; we are verifying that our username gets set as the application |
| 72 | + // secret when using UDS authentication. |
| 73 | + |
| 74 | + // Create two connected sockets. |
| 75 | + let (sock_a, _sock_b) = UnixStream::pair().unwrap(); |
| 76 | + let (cred_a, _cred_b) = (sock_a.peer_cred().unwrap(), _sock_b.peer_cred().unwrap()); |
| 77 | + |
| 78 | + let authenticator = UDSAuthenticator {}; |
| 79 | + |
| 80 | + let req_auth = RequestAuth::new("secret".into()); |
| 81 | + let conn_metadata = Some(ConnectionMetadata::PeerCredentials(cred_a)); |
| 82 | + |
| 83 | + let auth_name = authenticator |
| 84 | + .authenticate(&req_auth, conn_metadata) |
| 85 | + .expect("Failed to authenticate"); |
| 86 | + |
| 87 | + assert_eq!(auth_name.get_name(), whoami::username()); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn unsuccessful_authentication_no_user() { |
| 92 | + // This test should FAIL; we're trying to use a non-existent user for authentication. |
| 93 | + let authenticator = UDSAuthenticator {}; |
| 94 | + |
| 95 | + let req_auth = RequestAuth::new("secret".into()); |
| 96 | + let bogus_ucred = UCred { |
| 97 | + // 65535 is a special UID. This value is unused/avoided because it was the API error return |
| 98 | + // value when uid_t was 16 bits. We should be safe to use this to represent a user that |
| 99 | + // does not exist. |
| 100 | + uid: 65535, |
| 101 | + // 65534 is a special GID that many Linux distributions map to the group name |
| 102 | + // `nogroup`. |
| 103 | + gid: 65534, |
| 104 | + }; |
| 105 | + let conn_metadata = Some(ConnectionMetadata::PeerCredentials(bogus_ucred)); |
| 106 | + |
| 107 | + let auth_result = authenticator.authenticate(&req_auth, conn_metadata); |
| 108 | + |
| 109 | + assert_eq!(auth_result, Err(ResponseStatus::AuthenticationError)); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn unsuccessful_authentication_no_metadata() { |
| 114 | + // Create two connected sockets. |
| 115 | + let authenticator = UDSAuthenticator {}; |
| 116 | + let req_auth = RequestAuth::new("secret".into()); |
| 117 | + |
| 118 | + let conn_metadata = None; |
| 119 | + let auth_result = authenticator.authenticate(&req_auth, conn_metadata); |
| 120 | + assert_eq!(auth_result, Err(ResponseStatus::AuthenticationError)); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn unsuccessful_authentication_wrong_metadata() { |
| 125 | + // TODO: this test needs implementing when we have more than one metadata type in |
| 126 | + // `PeerCredentials::ConnectionMetadata`. At the moment, the compiler just complains with |
| 127 | + // an 'unreachable branch' message. |
| 128 | + } |
| 129 | +} |
0 commit comments