aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/rbw/main.rs
blob: e2f2e855cef2d21ac49877381a90f6df69bb42dd (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use anyhow::Context as _;

mod actions;
mod commands;
mod sock;

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

    let matches = clap::App::new("rbw")
        .about("Unofficial Bitwarden CLI")
        .author(clap::crate_authors!())
        .version(clap::crate_version!())
        .subcommand(
            clap::SubCommand::with_name("config")
                .about("Get or set configuration options")
                .subcommand(
                    clap::SubCommand::with_name("show").about(
                        "Show the values of all configuration settings",
                    ),
                )
                .subcommand(
                    clap::SubCommand::with_name("set")
                        .about("Set a configuration option")
                        .arg(
                            clap::Arg::with_name("key")
                                .required(true)
                                .help("Configuration key to set"),
                        )
                        .arg(
                            clap::Arg::with_name("value")
                                .required(true)
                                .help(
                                "Value to set the configuration option to",
                            ),
                        ),
                )
                .subcommand(
                    clap::SubCommand::with_name("unset")
                        .about("Reset a configuration option to its default")
                        .arg(
                            clap::Arg::with_name("key")
                                .required(true)
                                .help("Configuration key to unset"),
                        ),
                ),
        )
        .subcommand(
            clap::SubCommand::with_name("login")
                .about("Log in to the Bitwarden server"),
        )
        .subcommand(
            clap::SubCommand::with_name("unlock")
                .about("Unlock the local Bitwarden database"),
        )
        .subcommand(
            clap::SubCommand::with_name("sync")
                .about("Update the local copy of the Bitwarden database"),
        )
        .subcommand(
            clap::SubCommand::with_name("list")
                .about("List all entries in the local Bitwarden database")
                .arg(
                    clap::Arg::with_name("fields")
                        .long("fields")
                        .takes_value(true)
                        .use_delimiter(true)
                        .multiple(true)
                        .help(
                            "Fields to display. \
                            Available options are id, name, user, folder. \
                            Multiple fields will be separated by tabs.",
                        ),
                )
                .visible_alias("ls"),
        )
        .subcommand(
            clap::SubCommand::with_name("get")
                .about("Display the password for a given entry")
                .arg(
                    clap::Arg::with_name("name")
                        .required(true)
                        .help("Name of the entry to display"),
                )
                .arg(
                    clap::Arg::with_name("user")
                        .help("Username of the entry to display"),
                )
                .arg(
                    clap::Arg::with_name("full").long("full").help(
                        "Display the notes in addition to the password",
                    ),
                ),
        )
        .subcommand(
            clap::SubCommand::with_name("add")
                .about("Add a new password to the database")
                .long_about(
                    "Add a new password to the database\n\n\
                    This command will open a text editor to enter \
                    the password and notes. The editor to use is determined \
                    by the value of the $EDITOR environment variable. The \
                    first line will be saved as the password and the \
                    remainder will be saved as a note.",
                )
                .arg(
                    clap::Arg::with_name("name")
                        .required(true)
                        .help("Name of the password entry"),
                )
                .arg(
                    clap::Arg::with_name("user")
                        .help("Username for the password entry"),
                )
                .arg(
                    clap::Arg::with_name("uri")
                        .long("uri")
                        .takes_value(true)
                        .multiple(true)
                        .number_of_values(1)
                        .use_delimiter(false)
                        .help("URI for the password entry"),
                )
                .arg(
                    clap::Arg::with_name("folder")
                        .long("folder")
                        .takes_value(true)
                        .help("Folder for the password entry"),
                ),
        )
        .subcommand(
            clap::SubCommand::with_name("generate")
                .about("Generate a new password")
                .long_about(
                    "Generate a new password\n\n\
                    If given a password entry name, also save the generated \
                    password to the database.",
                )
                .arg(
                    clap::Arg::with_name("len")
                        .required(true)
                        .help("Length of the password to generate"),
                )
                .arg(
                    clap::Arg::with_name("name")
                        .help("Name of the password entry"),
                )
                .arg(
                    clap::Arg::with_name("user")
                        .help("Username for the password entry"),
                )
                .arg(
                    clap::Arg::with_name("uri")
                        .long("uri")
                        .takes_value(true)
                        .multiple(true)
                        .number_of_values(1)
                        .use_delimiter(false)
                        .help("URI for the password entry"),
                )
                .arg(
                    clap::Arg::with_name("folder")
                        .long("folder")
                        .takes_value(true)
                        .help("Folder for the password entry"),
                )
                .arg(
                    clap::Arg::with_name("no-symbols")
                        .long("no-symbols")
                        .help(
                            "Generate a password with no special characters",
                        ),
                )
                .arg(
                    clap::Arg::with_name("only-numbers")
                        .long("only-numbers")
                        .help(
                            "Generate a password consisting of only numbers",
                        ),
                )
                .arg(
                    clap::Arg::with_name("nonconfusables")
                        .long("nonconfusables")
                        .help(
                            "Generate a password without visually similar \
                            characters (useful for passwords intended to be \
                            written down)",
                        ),
                )
                .arg(clap::Arg::with_name("diceware").long("diceware").help(
                    "Generate a password of multiple dictionary \
                    words chosen from the EFF word list. The len \
                    parameter for this option will set the number \
                    of words to generate, rather than characters.",
                ))
                .group(clap::ArgGroup::with_name("password-type").args(&[
                    "no-symbols",
                    "only-numbers",
                    "nonconfusables",
                    "diceware",
                ]))
                .visible_alias("gen"),
        )
        .subcommand(
            clap::SubCommand::with_name("edit")
                .about("Modify an existing password")
                .long_about(
                    "Modify an existing password\n\n\
                    This command will open a text editor with the existing \
                    password and notes of the given entry for editing. \
                    The editor to use is determined  by the value of the \
                    $EDITOR environment variable. The first line will be \
                    saved as the password and the remainder will be saved \
                    as a note.",
                )
                .arg(
                    clap::Arg::with_name("name")
                        .required(true)
                        .help("Name of the password entry"),
                )
                .arg(
                    clap::Arg::with_name("user")
                        .help("Username for the password entry"),
                ),
        )
        .subcommand(
            clap::SubCommand::with_name("remove")
                .about("Remove a given entry")
                .arg(
                    clap::Arg::with_name("name")
                        .required(true)
                        .help("Name of the password entry"),
                )
                .arg(
                    clap::Arg::with_name("user")
                        .help("Username for the password entry"),
                )
                .visible_alias("rm"),
        )
        .subcommand(
            clap::SubCommand::with_name("history")
                .about("View the password history for a given entry")
                .arg(
                    clap::Arg::with_name("name")
                        .required(true)
                        .help("Name of the password entry"),
                )
                .arg(
                    clap::Arg::with_name("user")
                        .help("Username for the password entry"),
                ),
        )
        .subcommand(
            clap::SubCommand::with_name("lock")
                .about("Lock the password database"),
        )
        .subcommand(
            clap::SubCommand::with_name("purge")
                .about("Remove the local copy of the password database"),
        )
        .subcommand(
            clap::SubCommand::with_name("stop-agent")
                .about("Terminate the background agent")
                .visible_alias("logout"),
        )
        .get_matches();

    let res = match matches.subcommand() {
        ("config", Some(smatches)) => match smatches.subcommand() {
            ("show", Some(_)) => {
                commands::config_show().context("config show")
            }
            // these unwraps are fine because key and value are both marked
            // .required(true)
            ("set", Some(ssmatches)) => commands::config_set(
                ssmatches.value_of("key").unwrap(),
                ssmatches.value_of("value").unwrap(),
            )
            .context("config set"),
            // this unwrap is fine because key is marked .required(true)
            ("unset", Some(ssmatches)) => {
                commands::config_unset(ssmatches.value_of("key").unwrap())
                    .context("config unset")
            }
            _ => {
                eprintln!("{}", smatches.usage());
                std::process::exit(1);
            }
        },
        ("login", Some(_)) => commands::login().context("login"),
        ("unlock", Some(_)) => commands::unlock().context("unlock"),
        ("sync", Some(_)) => commands::sync().context("sync"),
        ("list", Some(smatches)) => commands::list(
            &smatches
                .values_of("fields")
                .map(|it| it.collect())
                .unwrap_or_else(|| vec!["name"]),
        )
        .context("list"),
        // this unwrap is safe because name is marked .required(true)
        ("get", Some(smatches)) => commands::get(
            smatches.value_of("name").unwrap(),
            smatches.value_of("user"),
            smatches.is_present("full"),
        )
        .context("get"),
        // this unwrap is safe because name is marked .required(true)
        ("add", Some(smatches)) => commands::add(
            smatches.value_of("name").unwrap(),
            smatches.value_of("user"),
            smatches
                .values_of("uri")
                .map(|it| it.collect())
                .unwrap_or_else(|| vec![]),
            smatches.value_of("folder"),
        )
        .context("add"),
        ("generate", Some(smatches)) => {
            let ty = if smatches.is_present("no-symbols") {
                rbw::pwgen::Type::NoSymbols
            } else if smatches.is_present("only-numbers") {
                rbw::pwgen::Type::Numbers
            } else if smatches.is_present("nonconfusables") {
                rbw::pwgen::Type::NonConfusables
            } else if smatches.is_present("diceware") {
                rbw::pwgen::Type::Diceware
            } else {
                rbw::pwgen::Type::AllChars
            };
            // this unwrap is fine because len is marked as .required(true)
            let len = smatches.value_of("len").unwrap();
            match len.parse() {
                Ok(len) => commands::generate(
                    smatches.value_of("name"),
                    smatches.value_of("user"),
                    smatches
                        .values_of("uri")
                        .map(|it| it.collect())
                        .unwrap_or_else(|| vec![]),
                    smatches.value_of("folder"),
                    len,
                    ty,
                )
                .context("generate"),
                Err(e) => Err(e.into()),
            }
        }
        // this unwrap is safe because name is marked .required(true)
        ("edit", Some(smatches)) => commands::edit(
            smatches.value_of("name").unwrap(),
            smatches.value_of("user"),
        )
        .context("edit"),
        // this unwrap is safe because name is marked .required(true)
        ("remove", Some(smatches)) => commands::remove(
            smatches.value_of("name").unwrap(),
            smatches.value_of("user"),
        )
        .context("remove"),
        // this unwrap is safe because name is marked .required(true)
        ("history", Some(smatches)) => commands::history(
            smatches.value_of("name").unwrap(),
            smatches.value_of("user"),
        )
        .context("history"),
        ("lock", Some(_)) => commands::lock().context("lock"),
        ("purge", Some(_)) => commands::purge().context("purge"),
        ("stop-agent", Some(_)) => {
            commands::stop_agent().context("stop-agent")
        }
        _ => {
            eprintln!("{}", matches.usage());
            std::process::exit(1);
        }
    }
    .context("rbw");

    if let Err(e) = res {
        eprintln!("{:#}", e);
        std::process::exit(1);
    }
}