aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/rbw/commands.rs
blob: 5f1c2f16db046e2ae99d29b8de59998446b7a690 (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
use anyhow::Context as _;

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 cipher in db.ciphers {
        println!(
            "{}",
            crate::actions::decrypt(&cipher.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
    );
    for cipher in db.ciphers {
        let cipher_name = crate::actions::decrypt(&cipher.name)
            .context("failed to decrypt entry name")?;
        if name == cipher_name {
            if let Some(user) = user {
                if let Some(encrypted_user) = &cipher.login.username {
                    let cipher_user = crate::actions::decrypt(encrypted_user)
                        .context("failed to decrypt entry username")?;
                    if user == cipher_user {
                        if let Some(encrypted_pass) = &cipher.login.password {
                            let pass =
                                crate::actions::decrypt(encrypted_pass)
                                    .context(
                                        "failed to decrypt entry password",
                                    )?;
                            println!("{}", pass);
                        } else {
                            eprintln!("no password found for entry {}", desc);
                        }
                        return Ok(());
                    }
                }
            } else {
                if let Some(encrypted_pass) = &cipher.login.password {
                    let pass = crate::actions::decrypt(encrypted_pass)
                        .context("failed to decrypt entry password")?;
                    println!("{}", pass);
                } else {
                    eprintln!("no password found for entry {}", desc);
                }
                return Ok(());
            }
        }
    }

    eprintln!("no entry found for {}", desc);
    Ok(())
}

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

    todo!()
}

pub fn generate(
    name: Option<&str>,
    user: Option<&str>,
    len: usize,
    ty: rbw::pwgen::Type,
) -> anyhow::Result<()> {
    let pw = rbw::pwgen::pwgen(ty, len);
    // unwrap is safe because pwgen is guaranteed to always return valid utf8
    println!("{}", std::str::from_utf8(pw.data()).unwrap());

    if name.is_some() && user.is_some() {
        unlock()?;

        todo!();
    }

    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 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"))
    }
}