|
| 1 | +// Copyright 2020 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 | + |
| 16 | +#[derive(Copy, Clone, Debug)] |
| 17 | +pub struct UDSAuthenticator; |
| 18 | + |
| 19 | +impl Authenticate for UDSAuthenticator { |
| 20 | + fn authenticate( |
| 21 | + &self, |
| 22 | + _auth: &RequestAuth, |
| 23 | + meta: Option<ConnectionMetadata>, |
| 24 | + ) -> Result<ApplicationName> { |
| 25 | + let meta = match meta { |
| 26 | + Some(meta) => meta, |
| 27 | + None => { |
| 28 | + error!("Authenticator did not receive any metadata; cannot authenticate."); |
| 29 | + return Err(ResponseStatus::AuthenticationError); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + let (uid, _gid) = match meta { |
| 34 | + ConnectionMetadata::PeerCredentials{uid, gid} => (uid, gid), |
| 35 | + // TODO: add wildcard pattern when `ConnectionMetadata` has more possibilities. |
| 36 | + }; |
| 37 | + |
| 38 | + Ok(ApplicationName(uid.to_string())) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[cfg(test)] |
| 43 | +mod test { |
| 44 | + use super::super::Authenticate; |
| 45 | + use super::UDSAuthenticator; |
| 46 | + use crate::front::listener::ConnectionMetadata; |
| 47 | + use parsec_interface::requests::request::RequestAuth; |
| 48 | + use parsec_interface::requests::ResponseStatus; |
| 49 | + use std::os::unix::net::UnixStream; |
| 50 | + use users::get_current_uid; |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn successful_authentication() { |
| 54 | + // This test should PASS; we are verifying that our username gets set as the application |
| 55 | + // secret when using UDS authentication. |
| 56 | + |
| 57 | + // Create two connected sockets. |
| 58 | + let (sock_a, _sock_b) = UnixStream::pair().unwrap(); |
| 59 | + let (cred_a, _cred_b) = (sock_a.peer_cred().unwrap(), _sock_b.peer_cred().unwrap()); |
| 60 | + |
| 61 | + let authenticator = UDSAuthenticator {}; |
| 62 | + |
| 63 | + let req_auth = RequestAuth::new("secret".into()); |
| 64 | + let conn_metadata = Some(ConnectionMetadata::PeerCredentials { |
| 65 | + uid: cred_a.uid, |
| 66 | + gid: cred_a.gid, |
| 67 | + }); |
| 68 | + |
| 69 | + let auth_name = authenticator |
| 70 | + .authenticate(&req_auth, conn_metadata) |
| 71 | + .expect("Failed to authenticate"); |
| 72 | + |
| 73 | + assert_eq!(auth_name.get_name(), get_current_uid().to_string()); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn unsuccessful_authentication_no_metadata() { |
| 78 | + // Create two connected sockets. |
| 79 | + let authenticator = UDSAuthenticator {}; |
| 80 | + let req_auth = RequestAuth::new("secret".into()); |
| 81 | + |
| 82 | + let conn_metadata = None; |
| 83 | + let auth_result = authenticator.authenticate(&req_auth, conn_metadata); |
| 84 | + assert_eq!(auth_result, Err(ResponseStatus::AuthenticationError)); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn unsuccessful_authentication_wrong_metadata() { |
| 89 | + // TODO: this test needs implementing when we have more than one metadata type in |
| 90 | + // `PeerCredentials::ConnectionMetadata`. At the moment, the compiler just complains with |
| 91 | + // an 'unreachable branch' message. |
| 92 | + } |
| 93 | +} |
0 commit comments