1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright (c) 2016 Boucher, Antoni <bouanto@zoho.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

mod chomp;

use std::error;
use std::ffi::OsStr;
use std::fmt::{self, Display, Formatter};
use std::io::{self, Write};
use std::process::{Command, Stdio};
use std::string;

use Error::*;
use chomp::Chomp;

macro_rules! validate_path {
    ($path:expr) => {
        if $path.trim().is_empty() {
            return Err(InvalidInput);
        }
    };
}

/// `Error` type that can be returned by the `PasswordStore` methods.
#[derive(Debug)]
pub enum Error {
    FromUtf8(string::FromUtf8Error),
    Io(io::Error),
    InvalidInput,
    Pass(String),
}

impl From<io::Error> for Error {
    fn from(error: io::Error) -> Self {
        Io(error)
    }
}

impl From<string::FromUtf8Error> for Error {
    fn from(error: string::FromUtf8Error) -> Self {
        FromUtf8(error)
    }
}

impl Display for Error {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        let string =
            match *self {
                FromUtf8(ref error) => error.to_string(),
                Io(ref error) => error.to_string(),
                InvalidInput => "invalid input".to_string(),
                Pass(ref error) => error.clone(),
            };
        write!(formatter, "{}", string)
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            FromUtf8(ref error) => error.description(),
            Io(ref error) => error.description(),
            InvalidInput => "invalid input",
            Pass(ref error) => error,
        }
    }
}

/// `Result` type returned by the `PasswordStore` methods.
pub type Result<T> = std::result::Result<T, Error>;

/// `Pass` process runner.
pub struct PasswordStore;

impl PasswordStore {
    /// Get the password a the specified `path`.
    pub fn get(path: &str) -> Result<String> {
        validate_path!(path);
        let mut password = exec_pass("", &[path], None)?;
        password.chomp();
        Ok(password)
    }

    /// Get the list of usernames at the specified `path`.
    pub fn get_usernames(path: &str) -> Result<Vec<String>> {
        validate_path!(path);
        let output = exec_pass("", &[path], None)?;
        let mut usernames = vec![];
        // Skip the first line since it is the path.
        for line in output.lines().skip(1) {
            // Skip the first four characters since they are not part of the identifier.
            usernames.push(line.chars().skip(4).collect());
        }
        Ok(usernames)
    }

    /// Insert a password in the store.
    pub fn insert(path: &str, password: &str) -> Result<()> {
        validate_path!(path);
        exec_pass("insert", &["-m", path], Some(password))?;
        Ok(())
    }

    /// Remove a password from the store.
    pub fn remove(path: &str) -> Result<()> {
        validate_path!(path);
        exec_pass("rm", &["-f", path], None)?;
        Ok(())
    }
}

/// Exec the `pass` process with the specified `command` and `args`.
fn exec_pass<S: AsRef<OsStr>>(command: &str, args: &[S], input: Option<&str>) -> Result<String> {
    let mut process = Command::new("pass");
    if !command.trim().is_empty() {
        process.arg(command);
    }
    let mut child = process.args(args)
        .stderr(Stdio::piped())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()?;
    if let (Some(stdin), Some(input)) = (child.stdin.as_mut(), input) {
        write!(stdin, "{}\n", input)?;
    }
    let output = child.wait_with_output()?;
    let mut stderr = String::from_utf8(output.stderr)?;
    if !stderr.is_empty() {
        stderr.chomp();
        Err(Pass(stderr))
    }
    else {
        Ok(String::from_utf8(output.stdout)?)
    }
}