keeprint/src/main.rs

77 lines
2.0 KiB
Rust
Raw Normal View History

2023-12-01 23:50:36 -07:00
/*
2024-04-28 20:30:25 -06:00
* Copyright (c) 20232024 Emma Tebibyte
2023-12-01 23:50:36 -07:00
* 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::{
2024-04-28 20:30:25 -06:00
env,
fs::File,
io::stdin,
process::exit,
2023-12-01 23:50:36 -07:00
};
use keepass::{
2024-04-28 20:30:25 -06:00
db::NodeRef,
Database,
DatabaseKey,
2023-12-01 23:50:36 -07:00
};
2024-04-28 20:30:25 -06:00
use rustexits::{ EX_TEMPFAIL, EX_UNAVAILABLE, EX_USAGE };
2023-12-01 23:50:36 -07:00
fn main() {
2024-04-28 20:30:25 -06:00
let argv = env::args().collect::<Vec<String>>();
2023-12-01 23:50:36 -07:00
2024-04-28 20:30:25 -06:00
if !argv.get(1).is_some() {
eprintln!("Usage: {} database", argv[0]);
exit(EX_USAGE);
}
2023-12-02 00:12:15 -07:00
2024-04-28 20:30:25 -06:00
let mut file = match File::open(&argv[1]) {
Ok(file) => file,
Err(err) => {
eprintln!("{}: {:?}", argv[0], err);
exit(1);
},
};
2023-12-01 23:50:36 -07:00
2024-04-28 20:30:25 -06:00
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-01 23:50:36 -07:00
2024-04-28 20:30:25 -06:00
let password = password.lines().nth(0).unwrap_or(&"");
2023-12-01 23:50:36 -07:00
2024-04-28 20:30:25 -06:00
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);
});
2023-12-01 23:50:36 -07:00
2024-04-28 20:30:25 -06:00
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)
},
}
}
2023-12-01 23:50:36 -07:00
}