aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/rbw-agent/agent.rs
blob: f092787505585ad7d59fb4d116973952871cf988 (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use anyhow::Context as _;
use tokio::stream::StreamExt as _;

pub struct State {
    pub priv_key: Option<rbw::locked::Keys>,
}

impl State {
    pub fn needs_unlock(&self) -> bool {
        self.priv_key.is_none()
    }
}

pub struct Agent {
    timeout: tokio::time::Delay,
    state: std::sync::Arc<tokio::sync::RwLock<State>>,
}

impl Agent {
    pub fn new() -> anyhow::Result<Self> {
        let config =
            rbw::config::Config::load().context("failed to load config")?;
        Ok(Self {
            timeout: tokio::time::delay_for(
                tokio::time::Duration::from_secs(config.lock_timeout),
            ),
            state: std::sync::Arc::new(tokio::sync::RwLock::new(State {
                priv_key: None,
            })),
        })
    }

    pub async fn run(
        &mut self,
        mut listener: tokio::net::UnixListener,
    ) -> anyhow::Result<()> {
        loop {
            tokio::select! {
                sock = listener.next() => {
                    let sock = if let Some(sock) = sock {
                        sock
                    } else {
                        return Ok(());
                    };
                    let mut sock = crate::sock::Sock::new(
                        sock.context("failed to accept incoming connection")?
                    );
                    let state = self.state.clone();
                    tokio::spawn(async move {
                        let res
                            = handle_request(&mut sock, state.clone()).await;
                        if let Err(e) = res {
                            // unwrap is the only option here
                            sock.send(&rbw::agent::Response::Error {
                                error: format!("{:#}", e),
                            }).await.unwrap();
                        }
                    });
                }
                _ = &mut self.timeout => {
                    let state = self.state.clone();
                    tokio::spawn(async move{
                        state.write().await.priv_key = None
                    });
                }
            }
        }
    }
}

async fn handle_request(
    sock: &mut crate::sock::Sock,
    state: std::sync::Arc<tokio::sync::RwLock<State>>,
) -> anyhow::Result<()> {
    let req = sock
        .recv()
        .await
        .context("failed to receive incoming message")?;
    match &req.action {
        rbw::agent::Action::Login => {
            crate::actions::login(sock, state.clone(), req.tty.as_deref())
                .await
        }
        rbw::agent::Action::Unlock => {
            crate::actions::unlock(sock, state.clone(), req.tty.as_deref())
                .await
        }
        rbw::agent::Action::Lock => {
            crate::actions::lock(sock, state.clone()).await
        }
        rbw::agent::Action::Sync => crate::actions::sync(sock).await,
        rbw::agent::Action::Decrypt { cipherstring } => {
            crate::actions::decrypt(sock, state.clone(), &cipherstring).await
        }
        rbw::agent::Action::Quit => std::process::exit(0),
    }
}