11 $0: a tool for dealing with passwords.
14 -f [path]: specify the password file to use.
15 -h: this help message.
16 -p: the input file is plaintext, and we should encrypt it.
17 -s [pattern]: search for the given pattern
19 Environment variables:
20 EDITOR: the editor to use.
24 TEMPDIR="/dev/shm/passtool.$$.$RANDOM"
26 mkdir -p "${TEMPDIR}" || die "failed to mkdir ${TEMPDIR}"
27 TEMPFILE="${TEMPDIR}/temp"
28 TEMPFILE_ENC="${TEMPDIR}/temp.nc"
29 trap "rm -rf ${TEMPDIR}; exit" EXIT
30 #chmod 007 "${TEMPDIR}" || die "failed to chmod ${TEMPDIR}"
34 encrypt_file "${TEMPFILE}" "${PASSWORD_PATH}"
35 [[ $? -ne 0 ]] && die "Failed to encrypt ${PASSWORD_PATH}"
36 mv -f "${TEMPFILE}" "${PASSWORD_PATH}" || \
37 die "Failed to replace ${PASSWORD_PATH}"
41 search_existing_file() {
43 decrypt_file "${TEMPFILE}" "${PASSWORD_PATH}"
45 die "Failed to decrypt ${PASSWORD_PATH}. Was the password correct?"
46 egrep ${SEARCH_PATTERN} "${TEMPFILE}"
49 edit_existing_file() {
51 decrypt_file "${TEMPFILE}" "${PASSWORD_PATH}"
53 die "Failed to decrypt ${PASSWORD_PATH}. Was the password correct?"
54 ${EDITOR} "${TEMPFILE}"
55 encrypt_file "${PASSWORD_PATH}" "${TEMPFILE}"
57 die "failed to move ${TEMPFILE_ENC} to ${PASSWORD_PATH}: changes lost."
64 openssl enc -aes-256-ecb -k "${PASSWORD}" -salt < "${src}" > "${dest}"
70 openssl enc -d -aes-256-ecb -k "${PASSWORD}" < "${src}" > "${dest}"
75 while getopts "f:hps:" flag; do
77 f) PASSWORD_PATH="${OPTARG}";;
80 s) SEARCH_PATTERN="${OPTARG}";;
81 *) echo; usage; exit 1;;
85 [[ -z ${PASSWORD_PATH} ]] && die "You must specify a password file path with -f"
86 [[ -f ${PASSWORD_PATH} ]] || die "No regular file found at ${PASSWORD_PATH}"
88 if [[ -z ${PASSWORD} ]]; then
89 read -s -p "enter password: " PASSWORD
91 if [[ ${PLAINTEXT} -eq 1 ]]; then
92 if [[ ${SEARCH_PATTERN} != "" ]]; then
93 die "You may not specify both -s and -p."
96 elif [[ ${SEARCH_PATTERN} != "" ]]; then