Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ target/
.DS_Store
.idea/
*.iml
site_counter.db
57 changes: 57 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ rpassword = "0.4"
ring = "0.9"
ring-pwhash = "0.2"
rustyline = "1.0"
rusqlite = "0.12.0"
libsqlite3-sys = "0.8.0"

[badges]
travis-ci = { repository = "lispyclouds/mpw-rs" }
18 changes: 16 additions & 2 deletions src/arg_parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ extern crate clap;
// along with Master Password. If not, see <http://www.gnu.org/licenses/>.

mod helpers;
mod store;

use std::process;
use self::clap::{Arg, App};
use common::{SiteVariant, SiteType};
use benchmark::mpw_bench;

#[derive(Debug)]
pub struct MpwOptions {
pub site: String,
pub user: String,
Expand Down Expand Up @@ -111,6 +113,11 @@ pub fn get_opts() -> MpwOptions {
.long("benchmark")
.help("Benchmarks this program")
.takes_value(false))
.arg(Arg::with_name("save")
.short("s")
.long("save")
.help("Saves the current counter value for the site")
.takes_value(false))
.get_matches();

if matches.is_present("benchmark") {
Expand All @@ -131,7 +138,7 @@ pub fn get_opts() -> MpwOptions {
let user = match helpers::read_opt(&matches, "user", "MP_FULLNAME") {
Some(val) => val.to_string(),
None => {
match helpers::raw_input("Site Name: ") {
match helpers::raw_input("Full Name: ") {
Some(val) => val,
None => panic!("Can't read STDIN"),
}
Expand Down Expand Up @@ -171,13 +178,20 @@ pub fn get_opts() -> MpwOptions {
None => String::new(),
};

MpwOptions {
let mpw_options = MpwOptions {
site: site,
user: user,
variant: variant.unwrap(),
template: template.unwrap(),
counter: counter,
algo: algo,
context: context,
};

if matches.is_present("save") {
store::save_to_sql_lite(mpw_options)
}
else {
mpw_options
}
}
61 changes: 61 additions & 0 deletions src/arg_parse/store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
extern crate rusqlite;

use self::rusqlite::Connection;
use arg_parse::MpwOptions;
use self::rusqlite::{Result};


pub fn save_to_sql_lite(mpw_options: MpwOptions) -> MpwOptions {
let conn = Connection::open("site_counter.db").unwrap();
conn.execute("CREATE TABLE IF NOT EXISTS mpwOptions (\
site TEXT NOT NULL,\
user TEXT NOT NULL,\
variant INTEGER,\
template INTEGER,\
counter INTEGER,\
algo TEXT NOT NULL,\
context TEXT NOT NULL\
)", &[]).unwrap();

let variant = mpw_options.variant.clone() as i32;
let template = mpw_options.template.clone() as i32;
let counter = mpw_options.counter as i32;
let mut updated_counter = counter;

let does_exist: Result<i32> = conn.query_row("SELECT counter FROM mpwOptions \
WHERE site = ? and user = ? and variant = ? and template = ?\
and algo = ? and context = ?", &[&mpw_options.site, &mpw_options.user, &variant, &template,
&mpw_options.algo, &mpw_options.context], |r| r.get(0));

let test: bool = match does_exist {
Ok(_) => true,
_ => false,
};

if !test {
conn.execute("INSERT INTO mpwOptions \
(site,user,variant,template,counter,algo,context)\
VALUES (?1,?2,?3,?4,?5,?6,?7)",
&[&mpw_options.site, &mpw_options.user, &variant, &template,
&counter, &mpw_options.algo, &mpw_options.context]).unwrap();
} else {
updated_counter = does_exist.unwrap() + 1;
println!("Current Site Counter is: {}",updated_counter);
conn.execute("UPDATE mpwOptions SET counter = ?\
WHERE site = ? and user = ? \
and variant = ? and template = ? \
and algo = ? and context = ?",
&[&updated_counter, &mpw_options.site, &mpw_options.user, &variant, &template,
&mpw_options.algo, &mpw_options.context]).unwrap();
}

MpwOptions {
site: mpw_options.site,
user: mpw_options.user,
variant: mpw_options.variant,
template: mpw_options.template,
counter: updated_counter,
algo: mpw_options.algo,
context: mpw_options.context,
}
}
4 changes: 4 additions & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use self::argon2rs::{Variant, Argon2};
pub const KEY_LENGTH: usize = 64_usize;

#[derive(PartialEq, Eq)]
#[derive(Debug)]
#[derive(Clone)]
pub enum SiteVariant {
Password,
Login,
Expand All @@ -40,6 +42,8 @@ impl SiteVariant {
}

#[derive(PartialEq, Eq)]
#[derive(Debug)]
#[derive(Clone)]
pub enum SiteType {
Maximum,
Long,
Expand Down