forked from bolcom/libunftp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.rs
63 lines (58 loc) · 1.93 KB
/
auth.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! The `AUTH` command used to support TLS
//!
//! A client requests TLS with the AUTH command and then decides if it
//! wishes to secure the data connections by use of the PBSZ and PROT
//! commands.
use crate::{
auth::UserDetail,
server::{
chancomms::ControlChanMsg,
controlchan::{
error::ControlChanError,
handler::{CommandContext, CommandHandler},
Reply, ReplyCode,
},
},
storage::{Metadata, StorageBackend},
};
use async_trait::async_trait;
// The parameter that can be given to the `AUTH` command.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum AuthParam {
Ssl,
Tls,
}
#[derive(Debug)]
pub struct Auth {
protocol: AuthParam,
}
impl Auth {
pub fn new(protocol: AuthParam) -> Self {
Auth { protocol }
}
}
#[async_trait]
impl<Storage, User> CommandHandler<Storage, User> for Auth
where
User: UserDetail + 'static,
Storage: StorageBackend<User> + 'static,
Storage::Metadata: Metadata,
{
#[tracing_attributes::instrument]
async fn handle(&self, args: CommandContext<Storage, User>) -> Result<Reply, ControlChanError> {
let tx = args.tx_control_chan.clone();
let logger = args.logger;
match (args.tls_configured, self.protocol.clone()) {
(true, AuthParam::Tls) => {
tokio::spawn(async move {
if let Err(err) = tx.send(ControlChanMsg::SecureControlChannel).await {
slog::warn!(logger, "AUTH: Could not send internal message to notify of TLS upgrade: {}", err);
}
});
Ok(Reply::new(ReplyCode::AuthOkayNoDataNeeded, "Upgrading to TLS"))
}
(true, AuthParam::Ssl) => Ok(Reply::new(ReplyCode::CommandNotImplementedForParameter, "Auth SSL not implemented")),
(false, _) => Ok(Reply::new(ReplyCode::CommandNotImplemented, "TLS/SSL not configured")),
}
}
}