keeprint/src/main.rs
2024-04-28 20:30:25 -06:00

77 lines
2.0 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 20232024 Emma Tebibyte
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
use std::{
env,
fs::File,
io::stdin,
process::exit,
};
use keepass::{
db::NodeRef,
Database,
DatabaseKey,
};
use rustexits::{ EX_TEMPFAIL, EX_UNAVAILABLE, EX_USAGE };
fn main() {
let argv = env::args().collect::<Vec<String>>();
if !argv.get(1).is_some() {
eprintln!("Usage: {} database", argv[0]);
exit(EX_USAGE);
}
let mut file = match File::open(&argv[1]) {
Ok(file) => file,
Err(err) => {
eprintln!("{}: {:?}", argv[0], err);
exit(1);
},
};
let mut password = String::new();
stdin().read_line(&mut password).unwrap_or_else(|_| {
eprintln!("{}: {}: No such file or directory.", argv[0], argv[1]);
exit(EX_UNAVAILABLE);
});
let password = password.lines().nth(0).unwrap_or(&"");
let key = DatabaseKey::new().with_password(password);
let db = Database::open(&mut file, key).unwrap_or_else(|_| {
eprintln!("{}: Incorrect key.", argv[0]);
exit(EX_TEMPFAIL);
});
for item in &db.root {
match item {
NodeRef::Group(_) => {},
NodeRef::Entry(entry) => {
let title = entry.get_title().unwrap_or("[title]");
let url = entry.get_url().unwrap_or("[url]");
let username = entry.get_username().unwrap_or("[username]");
let password = entry.get_password().unwrap_or("[password]");
println!("{}{}{}{}", title, url, username, password)
},
}
}
}