aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/rbw/commands.rs
blob: 30c5d1e40d39de7eac31197e2008495bc50b489f (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use anyhow::Context as _;

#[derive(Debug, Clone)]
struct DecryptedCipher {
    name: String,
    username: Option<String>,
    password: Option<String>,
    notes: Option<String>,
}

const HELP: &str = r#"
# The first line of this file will be the password, and the remainder of the
# file (after any blank lines after the password) will be stored as a note.
# Lines with leading # will be ignored.
"#;

pub fn config_show() -> anyhow::Result<()> {
    let config =
        rbw::config::Config::load().context("failed to load config")?;
    serde_json::to_writer_pretty(std::io::stdout(), &config)
        .context("failed to write config to stdout")?;
    println!();

    Ok(())
}

pub fn config_set(key: &str, value: &str) -> anyhow::Result<()> {
    let mut config = rbw::config::Config::load()
        .unwrap_or_else(|_| rbw::config::Config::new());
    match key {
        "email" => config.email = Some(value.to_string()),
        "base_url" => config.base_url = Some(value.to_string()),
        "identity_url" => config.identity_url = Some(value.to_string()),
        "lock_timeout" => {
            config.lock_timeout = value
                .parse()
                .context("failed to parse value for lock_timeout")?
        }
        _ => return Err(anyhow::anyhow!("invalid config key: {}", key)),
    }
    config.save().context("failed to save config file")?;

    Ok(())
}

pub fn login() -> anyhow::Result<()> {
    ensure_agent()?;
    crate::actions::login()?;

    Ok(())
}

pub fn unlock() -> anyhow::Result<()> {
    ensure_agent()?;
    crate::actions::login()?;
    crate::actions::unlock()?;

    Ok(())
}

pub fn sync() -> anyhow::Result<()> {
    ensure_agent()?;
    crate::actions::login()?;
    crate::actions::sync()?;

    Ok(())
}

pub fn list() -> anyhow::Result<()> {
    unlock()?;

    let email = config_email()?;
    let db = rbw::db::Db::load(&email)
        .context("failed to load password database")?;
    for entry in db.entries {
        println!(
            "{}",
            crate::actions::decrypt(&entry.name)
                .context("failed to decrypt entry name")?
        );
    }

    Ok(())
}

pub fn get(name: &str, user: Option<&str>) -> anyhow::Result<()> {
    unlock()?;

    let email = config_email()?;
    let db = rbw::db::Db::load(&email)
        .context("failed to load password database")?;

    let desc = format!(
        "{}{}",
        user.map(|s| format!("{}@", s))
            .unwrap_or_else(|| "".to_string()),
        name
    );

    let entry = find_entry(&db, name, user)
        .with_context(|| format!("couldn't find entry for '{}'", desc))?;
    if let Some(password) = entry.password {
        println!("{}", password);
    } else {
        eprintln!("entry for '{}' had no password", desc);
    }

    Ok(())
}

pub fn add(name: &str, username: Option<&str>) -> anyhow::Result<()> {
    unlock()?;

    let email = config_email()?;
    let mut db = rbw::db::Db::load(&email)?;
    // unwrap is safe here because the call to unlock above is guaranteed to
    // populate these or error
    let access_token = db.access_token.as_ref().unwrap();
    let refresh_token = db.refresh_token.as_ref().unwrap();

    let name = crate::actions::encrypt(name)?;

    let username = username
        .map(|username| crate::actions::encrypt(username))
        .transpose()?;

    let contents = rbw::edit::edit("", HELP)?;
    let mut lines = contents.lines();

    // XXX unwrap
    let password = lines.next().unwrap();
    let password = crate::actions::encrypt(password)?;

    let mut notes: String = lines
        .skip_while(|line| *line == "")
        .filter(|line| !line.starts_with('#'))
        .map(|line| format!("{}\n", line))
        .collect();
    while notes.ends_with('\n') {
        notes.pop();
    }
    let notes = if notes == "" {
        None
    } else {
        Some(crate::actions::encrypt(&notes)?)
    };

    if let Some(access_token) = rbw::actions::add(
        &access_token,
        &refresh_token,
        &name,
        username.as_deref(),
        Some(&password),
        notes.as_deref(),
    )? {
        db.access_token = Some(access_token);
        db.save(&email).context("failed to save database")?;
    }

    crate::actions::sync()?;

    Ok(())
}

pub fn generate(
    name: Option<&str>,
    username: Option<&str>,
    len: usize,
    ty: rbw::pwgen::Type,
) -> anyhow::Result<()> {
    let password = rbw::pwgen::pwgen(ty, len);
    println!("{}", password);

    if let Some(name) = name {
        unlock()?;

        let email = config_email()?;
        let mut db = rbw::db::Db::load(&email)?;
        // unwrap is safe here because the call to unlock above is guaranteed
        // to populate these or error
        let access_token = db.access_token.as_ref().unwrap();
        let refresh_token = db.refresh_token.as_ref().unwrap();

        let name = crate::actions::encrypt(name)?;
        let username = username
            .map(|username| crate::actions::encrypt(username))
            .transpose()?;
        let password = crate::actions::encrypt(&password)?;

        if let Some(access_token) = rbw::actions::add(
            &access_token,
            &refresh_token,
            &name,
            username.as_deref(),
            Some(&password),
            None,
        )? {
            db.access_token = Some(access_token);
            db.save(&email).context("failed to save database")?;
        }

        crate::actions::sync()?;
    }

    Ok(())
}

pub fn edit() -> anyhow::Result<()> {
    unlock()?;

    todo!()
}

pub fn remove() -> anyhow::Result<()> {
    unlock()?;

    todo!()
}

pub fn lock() -> anyhow::Result<()> {
    ensure_agent()?;
    crate::actions::lock()?;

    Ok(())
}

pub fn purge() -> anyhow::Result<()> {
    stop_agent()?;

    let email = config_email()?;
    rbw::db::Db::remove(&email).context("failed to remove database")?;

    Ok(())
}

pub fn stop_agent() -> anyhow::Result<()> {
    crate::actions::quit()?;

    Ok(())
}

fn ensure_agent() -> anyhow::Result<()> {
    let agent_path = std::env::var("RBW_AGENT");
    let agent_path = agent_path
        .as_ref()
        .map(|s| s.as_str())
        .unwrap_or("rbw-agent");
    let status = std::process::Command::new(agent_path)
        .status()
        .context("failed to run rbw-agent")?;
    if !status.success() {
        if let Some(code) = status.code() {
            if code != 23 {
                return Err(anyhow::anyhow!(
                    "failed to run rbw-agent: {}",
                    status
                ));
            }
        }
    }

    Ok(())
}

fn find_entry(
    db: &rbw::db::Db,
    name: &str,
    username: Option<&str>,
) -> anyhow::Result<DecryptedCipher> {
    let ciphers: anyhow::Result<Vec<DecryptedCipher>> = db
        .entries
        .iter()
        .cloned()
        .map(decrypt_cipher)
        .filter(|res| {
            if let Ok(decrypted_cipher) = res {
                name == decrypted_cipher.name
                    && if let Some(username) = username {
                        decrypted_cipher.username.as_deref() == Some(username)
                    } else {
                        true
                    }
            } else {
                true
            }
        })
        .collect();
    let ciphers = ciphers?;

    if ciphers.is_empty() {
        Err(anyhow::anyhow!("no entry found"))
    } else if ciphers.len() > 1 {
        let users: Vec<String> = ciphers
            .iter()
            .map(|cipher| {
                cipher
                    .username
                    .clone()
                    .unwrap_or_else(|| "(no login)".to_string())
            })
            .collect();
        let users = users.join(", ");
        Err(anyhow::anyhow!("multiple entries found: {}", users))
    } else {
        Ok(ciphers[0].clone())
    }
}

fn decrypt_cipher(entry: rbw::db::Entry) -> anyhow::Result<DecryptedCipher> {
    Ok(DecryptedCipher {
        name: crate::actions::decrypt(&entry.name)?,
        username: entry
            .username
            .as_ref()
            .map(|username| crate::actions::decrypt(username))
            .transpose()?,
        password: entry
            .password
            .as_ref()
            .map(|password| crate::actions::decrypt(password))
            .transpose()?,
        notes: entry
            .notes
            .as_ref()
            .map(|notes| crate::actions::decrypt(notes))
            .transpose()?,
    })
}

fn config_email() -> anyhow::Result<String> {
    let config = rbw::config::Config::load()?;
    if let Some(email) = config.email {
        Ok(email)
    } else {
        Err(anyhow::anyhow!("failed to find email address in config"))
    }
}