aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/agent.rs
blob: d56e5a0b56121a9ce5d31c4dfe31904307fb561d (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use tokio::io::{AsyncBufReadExt as _, AsyncWriteExt 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 send_response(
    sock: &mut tokio::net::UnixStream,
    res: &rbw::agent::Response,
) {
    sock.write_all(serde_json::to_string(res).unwrap().as_bytes())
        .await
        .unwrap();
    sock.write_all(b"\n").await.unwrap();
}

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

async fn login(
    sock: &mut tokio::net::UnixStream,
    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 keys =
        rbw::actions::unlock(email, &password, iterations, protected_key)
            .await
            .unwrap();
    state.priv_key = Some(keys);

    send_response(sock, &rbw::agent::Response::Ack).await;
}

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

async fn unlock(
    sock: &mut tokio::net::UnixStream,
    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 keys = rbw::actions::unlock(
        email,
        &password,
        state.iterations.unwrap(),
        state.protected_key.as_ref().unwrap().to_string(),
    )
    .await
    .unwrap();
    state.priv_key = Some(keys);

    send_response(sock, &rbw::agent::Response::Ack).await;
}

async fn sync(
    sock: &mut tokio::net::UnixStream,
    state: std::sync::Arc<tokio::sync::RwLock<State>>,
) {
    ensure_login(sock, 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;

    send_response(sock, &rbw::agent::Response::Ack).await;
}

async fn decrypt(
    sock: &mut tokio::net::UnixStream,
    state: std::sync::Arc<tokio::sync::RwLock<State>>,
    cipherstring: &str,
) {
    ensure_unlock(sock, state.clone()).await;
    let state = state.read().await;
    let keys = state.priv_key.as_ref().unwrap();
    let cipherstring =
        rbw::cipherstring::CipherString::new(cipherstring).unwrap();
    let plaintext =
        String::from_utf8(cipherstring.decrypt(keys).unwrap()).unwrap();

    send_response(sock, &rbw::agent::Response::Decrypt { plaintext }).await;
}

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

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

struct State {
    access_token: Option<String>,
    priv_key: Option<rbw::locked::Keys>,

    // 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;
    })
}