I'm writing a bash
script, and need to ask user for his password and pass it to openssl
. Whilst openssl
can read the password itself, I need for two runs of the program and don't want to ask the user twice. Here is the script:
cp file{,.old}
read -sp 'Enter password. ' PASS; echo
export PASS
# decode | edit | encode
openssl enc -d -aes-256-cbc -k "$PASS" -in file.old | \
sed ... | openssl enc -e -aes-256-cbc -k "$PASS" -out file
unset PASS
This is not safe as the password is easily available by looking at the command line; somebody can read it using ps
, for example.
openssl
can read a password from an environment variable, so I can replace -k "$PASS"
with -pass env:PASS
, but it's still not safe; the environment variables of any process can be read freely (again, ps
can do it).
So, how can I safely pass the password to the two openssl
instances?
ps
reads the environment of a process from/proc/<pid>/environ
, but this file has0600
permissions, so only root and the user running the process are able to read the environment of the process. I'd say that's pretty safe. – Martin von Wittich Dec 02 '16 at 17:01