2019-03-06 09:21:45 -05:00
|
|
|
//! Deletes (address, key)-binding(s), and/or a key(s).
|
|
|
|
|
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
extern crate failure;
|
|
|
|
use failure::Fallible as Result;
|
|
|
|
extern crate structopt;
|
|
|
|
use structopt::StructOpt;
|
|
|
|
|
|
|
|
extern crate hagrid_database as database;
|
2019-04-26 18:21:30 -04:00
|
|
|
use database::{Query, Database, KeyDatabase};
|
2019-03-06 09:21:45 -05:00
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
#[structopt(
|
|
|
|
name = "hagrid-delete",
|
|
|
|
about = "Deletes (address, key)-binding(s), and/or a key(s)."
|
|
|
|
)]
|
|
|
|
pub struct Opt {
|
|
|
|
/// Base directory.
|
|
|
|
#[structopt(parse(from_os_str))]
|
|
|
|
base: PathBuf,
|
|
|
|
|
|
|
|
/// E-Mail address, Fingerprint, or KeyID of the TPK to delete.
|
|
|
|
/// If a Fingerprint or KeyID is given, --all is implied.
|
|
|
|
query: String,
|
|
|
|
|
|
|
|
/// Also delete all bindings.
|
|
|
|
#[structopt(long = "all-bindings")]
|
|
|
|
all_bindings: bool,
|
|
|
|
|
|
|
|
/// Also delete all bindings and the key.
|
|
|
|
#[structopt(long = "all")]
|
|
|
|
all: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
if let Err(e) = real_main() {
|
|
|
|
let mut cause = e.as_fail();
|
|
|
|
eprint!("{}", cause);
|
|
|
|
while let Some(c) = cause.cause() {
|
|
|
|
eprint!(":\n {}", c);
|
|
|
|
cause = c;
|
|
|
|
}
|
|
|
|
eprintln!();
|
|
|
|
::std::process::exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn real_main() -> Result<()> {
|
|
|
|
let opt = Opt::from_args();
|
2019-04-26 18:21:30 -04:00
|
|
|
let db = KeyDatabase::new_from_base(opt.base.canonicalize()?)?;
|
2019-03-06 09:21:45 -05:00
|
|
|
delete(&db, &opt.query.parse()?, opt.all_bindings, opt.all)
|
|
|
|
}
|
|
|
|
|
2019-04-26 18:21:30 -04:00
|
|
|
fn delete(db: &KeyDatabase, query: &Query, all_bindings: bool, mut all: bool)
|
2019-03-06 09:21:45 -05:00
|
|
|
-> Result<()> {
|
|
|
|
match query {
|
|
|
|
Query::ByFingerprint(_) | Query::ByKeyID(_) => {
|
2019-03-07 13:17:51 -05:00
|
|
|
eprintln!("Fingerprint or KeyID given, deleting key and all \
|
|
|
|
bindings.");
|
2019-03-06 09:21:45 -05:00
|
|
|
all = true;
|
|
|
|
},
|
|
|
|
Query::ByEmail(_) => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
let tpk = db.lookup(&query)?.ok_or_else(
|
|
|
|
|| failure::format_err!("No TPK matching {:?}", query))?;
|
|
|
|
|
|
|
|
let fp: database::types::Fingerprint = tpk.fingerprint().try_into()?;
|
|
|
|
let mut results = Vec::new();
|
|
|
|
|
|
|
|
// First, delete the bindings.
|
|
|
|
if all_bindings || all {
|
2019-03-07 13:17:51 -05:00
|
|
|
results.push(
|
|
|
|
("all bindings".into(),
|
2019-04-27 18:05:49 -04:00
|
|
|
db.set_email_unpublished_all(&fp)));
|
2019-03-06 09:21:45 -05:00
|
|
|
} else {
|
|
|
|
if let Query::ByEmail(ref email) = query {
|
2019-04-27 18:05:49 -04:00
|
|
|
results.push(
|
|
|
|
(email.to_string(),
|
|
|
|
db.set_email_unpublished(&fp, email)));
|
2019-03-06 09:21:45 -05:00
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now delete the key(s) itself.
|
|
|
|
if all {
|
2019-04-27 18:05:49 -04:00
|
|
|
// TODO
|
|
|
|
/*for skb in tpk.subkeys() {
|
2019-03-06 09:21:45 -05:00
|
|
|
results.push(
|
|
|
|
(skb.subkey().fingerprint().to_keyid().to_string(),
|
|
|
|
db.unlink_kid(&skb.subkey().fingerprint().try_into()?,
|
|
|
|
&fp)));
|
|
|
|
results.push(
|
|
|
|
(skb.subkey().fingerprint().to_string(),
|
|
|
|
db.unlink_fpr(&skb.subkey().fingerprint().try_into()?,
|
|
|
|
&fp)));
|
|
|
|
}
|
|
|
|
|
|
|
|
results.push(
|
|
|
|
(tpk.fingerprint().to_keyid().to_string(),
|
|
|
|
db.unlink_kid(&tpk.fingerprint().try_into()?,
|
|
|
|
&fp)));
|
|
|
|
results.push(
|
|
|
|
(tpk.fingerprint().to_string(),
|
|
|
|
db.update(&fp, None)));
|
2019-04-27 18:05:49 -04:00
|
|
|
*/
|
2019-03-06 09:21:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut err = Ok(());
|
|
|
|
for (slug, result) in results {
|
|
|
|
eprintln!("{}: {}", slug,
|
|
|
|
if let Err(ref e) = result {
|
|
|
|
e.to_string()
|
|
|
|
} else {
|
|
|
|
"Deleted".into()
|
|
|
|
});
|
|
|
|
if err.is_ok() {
|
|
|
|
if let Err(e) = result {
|
|
|
|
err = Err(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err
|
|
|
|
}
|