aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/agent.rs
blob: f21abcc66289d3cfba11a72be2117e7797b6a0c3 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use tokio::io::AsyncBufReadExt as _;
use tokio::stream::StreamExt as _;

fn make_socket() -> anyhow::Result<tokio::net::UnixListener> {
    let runtime_dir = rbw::dirs::runtime_dir();
    std::fs::create_dir_all(&runtime_dir)?;

    let path = runtime_dir.join("socket");
    std::fs::remove_file(&path)?;
    let sock = tokio::net::UnixListener::bind(&path)?;
    log::debug!("listening on socket {}", path.to_string_lossy());
    Ok(sock)
}

async fn ensure_login(state: std::sync::Arc<tokio::sync::RwLock<State>>) {
    let rstate = state.read().await;
    if rstate.access_token.is_none() {
        login(state.clone(), None).await; // tty
    }
}

async fn login(
    state: std::sync::Arc<tokio::sync::RwLock<State>>,
    tty: Option<&str>,
) {
    let mut state = state.write().await;
    let email = "bitwarden@tozt.net"; // XXX read from config
    let password =
        rbw::pinentry::getpin("prompt", "desc", tty).await.unwrap();
    let (access_token, iterations, protected_key) =
        rbw::actions::login(email, &password).await.unwrap();
    state.access_token = Some(access_token);
    state.iterations = Some(iterations);
    let (enc_key, mac_key) =
        rbw::actions::unlock(email, &password, iterations, protected_key)
            .await
            .unwrap();
    state.priv_key = Some((enc_key, mac_key));
}

async fn ensure_unlock(state: std::sync::Arc<tokio::sync::RwLock<State>>) {
    let rstate = state.read().await;
    if rstate.priv_key.is_none() {
        unlock(state.clone(), None).await; // tty
    }
}

async fn unlock(
    state: std::sync::Arc<tokio::sync::RwLock<State>>,
    tty: Option<&str>,
) {
    let mut state = state.write().await;
    let email = "bitwarden@tozt.net"; // XXX read from config
    let password =
        rbw::pinentry::getpin("prompt", "desc", tty).await.unwrap();
    let (enc_key, mac_key) = rbw::actions::unlock(
        email,
        &password,
        state.iterations.unwrap(),
        state.protected_key.as_ref().unwrap().to_string(),
    )
    .await
    .unwrap();
    state.priv_key = Some((enc_key, mac_key));
}

async fn sync(state: std::sync::Arc<tokio::sync::RwLock<State>>) {
    ensure_login(state.clone()).await;
    let mut state = state.write().await;
    let (protected_key, ciphers) =
        rbw::actions::sync(state.access_token.as_ref().unwrap())
            .await
            .unwrap();
    state.protected_key = Some(protected_key);
    println!("{}", serde_json::to_string(&ciphers).unwrap());
    state.ciphers = ciphers;
}

async fn decrypt(
    state: std::sync::Arc<tokio::sync::RwLock<State>>,
    cipherstring: &str,
) {
    ensure_unlock(state.clone()).await;
    let state = state.read().await;
    let (enc_key, mac_key) = state.priv_key.as_ref().unwrap();
    let cipherstring =
        rbw::cipherstring::CipherString::new(cipherstring).unwrap();
    let plain = cipherstring.decrypt(&enc_key, &mac_key).unwrap();
    println!("{}", String::from_utf8(plain).unwrap());
}

async fn handle_sock(
    sock: tokio::net::UnixStream,
    state: std::sync::Arc<tokio::sync::RwLock<State>>,
) {
    let buf = tokio::io::BufStream::new(sock);
    let mut lines = buf.lines();
    while let Some(line) = lines.next().await {
        let line = line.unwrap();
        let msg: rbw::agent::Message = serde_json::from_str(&line).unwrap();
        match msg.action {
            rbw::agent::Action::Login => {
                login(state.clone(), msg.tty.as_deref()).await
            }
            rbw::agent::Action::Unlock => {
                unlock(state.clone(), msg.tty.as_deref()).await
            }
            rbw::agent::Action::Sync => sync(state.clone()).await,
            rbw::agent::Action::Decrypt { cipherstring } => {
                decrypt(state.clone(), &cipherstring).await
            }
        }
    }
}

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

struct State {
    access_token: Option<String>,
    priv_key: Option<(Vec<u8>, Vec<u8>)>,

    // these should be in a state file
    iterations: Option<u32>,
    protected_key: Option<String>,
    ciphers: Vec<rbw::api::Cipher>,
}

impl Agent {
    fn new() -> Self {
        Self {
            timeout: tokio::time::delay_for(
                tokio::time::Duration::from_secs(600), // read from config
            ),
            state: std::sync::Arc::new(tokio::sync::RwLock::new(State {
                access_token: None,
                iterations: None,
                protected_key: None,
                priv_key: None,
                ciphers: vec![],
            })),
        }
    }

    async fn run(&mut self, mut listener: tokio::net::UnixListener) {
        loop {
            tokio::select! {
                sock = listener.next() => {
                    let state = self.state.clone();
                    tokio::spawn(async move {
                        handle_sock(sock.unwrap().unwrap(), state).await
                    });
                }
                _ = &mut self.timeout => {
                    break;
                }
            }
        }
    }
}

fn main() {
    env_logger::from_env(
        env_logger::Env::default().default_filter_or("info"),
    )
    .init();

    let runtime_dir = rbw::dirs::runtime_dir();
    std::fs::create_dir_all(&runtime_dir).unwrap();

    let (r, w) = nix::unistd::pipe().unwrap();
    let res = daemonize::Daemonize::new()
        .pid_file(runtime_dir.join("pidfile"))
        .exit_action(move || {
            nix::unistd::close(w).unwrap();
            let mut buf = [0; 1];
            nix::unistd::read(r, &mut buf).unwrap();
            nix::unistd::close(r).unwrap();
        })
        .start();
    nix::unistd::close(r).unwrap();

    match res {
        Ok(_) => (),
        Err(e) => {
            match e {
                daemonize::DaemonizeError::LockPidfile(_) => {
                    // this means that there is already an agent running, so
                    // return a special exit code to allow the cli to detect
                    // this case and not error out
                    std::process::exit(23);
                }
                _ => panic!("failed to daemonize: {}", e),
            }
        }
    }

    tokio::runtime::Runtime::new().unwrap().block_on(async {
        let listener = make_socket();

        nix::unistd::write(w, &[0]).unwrap();
        nix::unistd::close(w).unwrap();

        let mut agent = Agent::new();
        agent.run(listener.unwrap()).await;
    })
}