keeprint/src/main.rs

76 lines
2.3 KiB
Rust
Raw Normal View History

2023-12-01 23:50:36 -07:00
/*
* Copyright (c) 2023 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,
};
use keepass::{
db::NodeRef,
Database,
DatabaseKey,
};
2023-12-02 00:12:15 -07:00
use yacexits::{ EX_TEMPFAIL, EX_UNAVAILABLE, EX_USAGE, exit };
2023-12-01 23:50:36 -07:00
fn main() {
let argv = env::args().collect::<Vec<String>>();
2023-12-02 00:12:15 -07:00
if !argv.get(1).is_some() {
eprintln!("Usage: {} database", argv[0]);
exit(EX_USAGE);
}
2023-12-01 23:50:36 -07:00
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);
});
2023-12-02 00:12:15 -07:00
let password = password.lines().nth(0).unwrap_or(&"");
2023-12-01 23:50:36 -07:00
2023-12-02 00:12:15 -07:00
let key = DatabaseKey::new().with_password(password);
2023-12-01 23:50:36 -07:00
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)
},
}
}
}