|
|
|
@ -18,16 +18,19 @@ static USAGE: &'static str = "
|
|
|
|
|
foil
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
foil set [--db=<db>]
|
|
|
|
|
foil get [--db=<db>]
|
|
|
|
|
foil list [--db=<db>]
|
|
|
|
|
foil set [--namespace=<ns>] [--db=<db>]
|
|
|
|
|
foil get [--namespace=<ns>] [--db=<db>]
|
|
|
|
|
foil list [--namespace=<ns>] [--db=<db>]
|
|
|
|
|
foil transfer [--db=<db>]
|
|
|
|
|
foil dump [--db=<db>]
|
|
|
|
|
foil generate <spec>
|
|
|
|
|
foil (-h | --help)
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
--db=<db> The path to the sqlite database [default: db.sqlite3].
|
|
|
|
|
-h --help Show this screen.
|
|
|
|
|
--version Show version.
|
|
|
|
|
--db=<db> The path to the sqlite database [default: db.sqlite3].
|
|
|
|
|
-n DB, --namespace=<db> An identifier for a group of secrets [default: main]
|
|
|
|
|
-h, --help Show this screen.
|
|
|
|
|
--version Show version.
|
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
@ -36,7 +39,10 @@ struct Args {
|
|
|
|
|
cmd_get: bool,
|
|
|
|
|
cmd_list: bool,
|
|
|
|
|
cmd_generate: bool,
|
|
|
|
|
cmd_transfer: bool,
|
|
|
|
|
cmd_dump: bool,
|
|
|
|
|
flag_db: Option<String>,
|
|
|
|
|
flag_namespace: String,
|
|
|
|
|
arg_spec: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -61,10 +67,6 @@ fn get_master_key(db_conn: &mut db::DbHandle) -> [u8; 32] {
|
|
|
|
|
let master_key: [u8; 32] = {
|
|
|
|
|
let master_password = PasswordInput::with_theme(&ColorfulTheme::default())
|
|
|
|
|
.with_prompt("Master password")
|
|
|
|
|
.with_confirmation(
|
|
|
|
|
"Repeat master password",
|
|
|
|
|
"Error: the passwords don't match.",
|
|
|
|
|
)
|
|
|
|
|
.interact()
|
|
|
|
|
.unwrap();
|
|
|
|
|
crypt::get_master_key(&db_conn, &master_password).unwrap()
|
|
|
|
@ -90,32 +92,33 @@ fn get_master_key(db_conn: &mut db::DbHandle) -> [u8; 32] {
|
|
|
|
|
master_key
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn list(mut db_conn: db::DbHandle, master_key: [u8; 32]) {
|
|
|
|
|
for host in db_conn
|
|
|
|
|
.list_accounts(master_key)
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|account: db::Account| account.host)
|
|
|
|
|
{
|
|
|
|
|
println!("{}", host);
|
|
|
|
|
fn list(mut db_conn: db::DbHandle, master_key: [u8; 32], namespace: &str) {
|
|
|
|
|
for note in db_conn.read_notes(master_key).unwrap() {
|
|
|
|
|
if note.namespace == namespace && note.category == "account" {
|
|
|
|
|
println!("{}", note.title);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get(mut db_conn: db::DbHandle, master_key: [u8; 32]) {
|
|
|
|
|
fn get(mut db_conn: db::DbHandle, master_key: [u8; 32], namespace: &str) {
|
|
|
|
|
println!("Reading a site from the database");
|
|
|
|
|
let host: String = Input::with_theme(&ColorfulTheme::default())
|
|
|
|
|
.with_prompt("hostname")
|
|
|
|
|
.interact()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
for account in db_conn.list_accounts(master_key) {
|
|
|
|
|
if account.host == host {
|
|
|
|
|
println!("username: {}", account.user);
|
|
|
|
|
println!("password: {}", account.password);
|
|
|
|
|
for note in db_conn.read_notes(master_key).unwrap() {
|
|
|
|
|
if note.namespace == namespace && note.title == host && note.category == "account" {
|
|
|
|
|
println!("===== note =====");
|
|
|
|
|
println!("namespace: {}", note.namespace);
|
|
|
|
|
println!("category: {}", note.category);
|
|
|
|
|
println!("title: {}", note.title);
|
|
|
|
|
println!("{}", note.value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set(mut db_conn: db::DbHandle, master_key: [u8; 32]) {
|
|
|
|
|
fn set(mut db_conn: db::DbHandle, master_key: [u8; 32], namespace: &str) {
|
|
|
|
|
println!("Adding a site to the database");
|
|
|
|
|
let host: String = Input::with_theme(&ColorfulTheme::default())
|
|
|
|
|
.with_prompt("hostname")
|
|
|
|
@ -125,21 +128,50 @@ fn set(mut db_conn: db::DbHandle, master_key: [u8; 32]) {
|
|
|
|
|
.with_prompt("username")
|
|
|
|
|
.interact()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let encrypted_host: crypt::EncryptedValue = crypt::encrypt_value(&host, master_key);
|
|
|
|
|
let encrypted_username: crypt::EncryptedValue = crypt::encrypt_value(&username, master_key);
|
|
|
|
|
let encrypted_password: crypt::EncryptedValue = {
|
|
|
|
|
let password: String = PasswordInput::with_theme(&ColorfulTheme::default())
|
|
|
|
|
.with_prompt("Site password")
|
|
|
|
|
.with_confirmation("Repeat site password", "Error: the passwords don't match.")
|
|
|
|
|
.interact()
|
|
|
|
|
.unwrap();
|
|
|
|
|
crypt::encrypt_value(&password, master_key)
|
|
|
|
|
};
|
|
|
|
|
db_conn.delete_account_with_host(master_key, &host);
|
|
|
|
|
let _account_id = db_conn.write_account(encrypted_host, encrypted_username, encrypted_password);
|
|
|
|
|
let password: String = PasswordInput::with_theme(&ColorfulTheme::default())
|
|
|
|
|
.with_prompt("Site password")
|
|
|
|
|
.with_confirmation("Repeat site password", "Error: the passwords don't match.")
|
|
|
|
|
.interact()
|
|
|
|
|
.unwrap();
|
|
|
|
|
db_conn.write_note(
|
|
|
|
|
master_key,
|
|
|
|
|
db::Note {
|
|
|
|
|
id: 0,
|
|
|
|
|
namespace: namespace.to_string(),
|
|
|
|
|
category: "account".to_string(),
|
|
|
|
|
title: host,
|
|
|
|
|
value: format!("username: {}\npassword: {}\n", username, password),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
println!("Successfully added password");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn dump(mut db_conn: db::DbHandle, master_key: [u8; 32]) {
|
|
|
|
|
for note in db_conn.read_notes(master_key).unwrap() {
|
|
|
|
|
println!("===== note =====");
|
|
|
|
|
println!("namespace: {}", note.namespace);
|
|
|
|
|
println!("category: {}", note.category);
|
|
|
|
|
println!("title: {}", note.title);
|
|
|
|
|
println!("{}", note.value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn transfer(mut db_conn: db::DbHandle, master_key: [u8; 32]) {
|
|
|
|
|
for account in db_conn.list_accounts(master_key).into_iter() {
|
|
|
|
|
let new_note = db::Note {
|
|
|
|
|
id: 0,
|
|
|
|
|
namespace: "main".to_owned(),
|
|
|
|
|
category: "account".to_owned(),
|
|
|
|
|
title: account.host,
|
|
|
|
|
value: format!(
|
|
|
|
|
"username: {}\npassword: {}\n",
|
|
|
|
|
account.user, account.password
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
db_conn.write_note(master_key, new_note);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
|
pretty_env_logger::init();
|
|
|
|
|
let args: Args = Docopt::new(USAGE)
|
|
|
|
@ -157,11 +189,15 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
|
let master_key: [u8; 32] = get_master_key(&mut db_conn);
|
|
|
|
|
|
|
|
|
|
if args.cmd_set {
|
|
|
|
|
set(db_conn, master_key);
|
|
|
|
|
set(db_conn, master_key, &args.flag_namespace);
|
|
|
|
|
} else if args.cmd_get {
|
|
|
|
|
get(db_conn, master_key);
|
|
|
|
|
get(db_conn, master_key, &args.flag_namespace);
|
|
|
|
|
} else if args.cmd_list {
|
|
|
|
|
list(db_conn, master_key);
|
|
|
|
|
list(db_conn, master_key, &args.flag_namespace);
|
|
|
|
|
} else if args.cmd_transfer {
|
|
|
|
|
transfer(db_conn, master_key);
|
|
|
|
|
} else if args.cmd_dump {
|
|
|
|
|
dump(db_conn, master_key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|