Skip to content

Instantly share code, notes, and snippets.

@bugabinga
Last active November 20, 2017 20:21
Show Gist options
  • Save bugabinga/fef9b91d5df16c2be9352a9cfcd86bfb to your computer and use it in GitHub Desktop.
Save bugabinga/fef9b91d5df16c2be9352a9cfcd86bfb to your computer and use it in GitHub Desktop.
This bash script converts simple pass files to bitwarden. Check the comments to understand what "simple" means ;)
#!/usr/bin/env bash
function pass_to_bitwarden(){
local path_to_pass="$1"
local output_path="$2"
#echo "searching path $path_to_pass for simple logins"
local url
local username
local decrypted_stuff
local password
local notes
#echo "Initialized the csv file header for bitwarden (individual account)."
echo "folder,favorite,type,name,notes,fields,login_uri,login_username,login_password,login_totp" > "$output_path"
for folder in "${path_to_pass%/}"/*; do
if [ -d "$folder" ]; then
#echo "found potential match for url: $folder"
url="$(basename "$folder")"
# best URL regex, I know...
# comment this if-statement out, if you don´t name your folders after domains. the 'login_url' will still be set though...
if [[ "$url" == *"."* ]]; then
for file in "${folder%/}"/*; do
#echo "found potential match for username: $file"
username="$(basename --suffix ".gpg" "$file")"
if [ -f "$file" ] ; then
decrypted_stuff="$(pass "$url"/"$username" 2> /dev/null)"
password="$(echo "$decrypted_stuff" | head -n +1)"
notes="$(echo "$decrypted_stuff" | tail -n +2)"
#echo "the AI has deterimed, that;"
#echo "URL: $url"
#echo "Username: $username"
#echo "Notes: $notes"
#echo "Password is BEEEEEEP!"
#echo
echo "passwordstore,0,login,$url,\"$notes\",,$url,$username,\"$password\"," >> "$output_path"
mkdir -p "${path_to_pass%/}"/Archive
pass mv "$url"/"$username" Archive/"$url"/"$username"
fi
done
fi
fi
done
}
if [ -z "${1// }" ] || [ -z "${2// }" ]; then
echo "Converts simple pass files to bitwarden(csv)."
echo "Usage: pass_to_bitwarden <path to password store> <path to csv output>"
exit -1
fi
# This script will scan the given path for password store logins in a simple format, and in this format only!
# Simple logins are assumed to be '<directory>/<file>' with the file content being lines of text.
# The directory name will be used as the 'login_uri', the file name as the 'login_username' and the first line of text as 'login_password'.
# Additional lines of text will be stored as 'notes'.
# Logins that do not fit this scheme will be ignored. Those that fit will be printed in CSV format that bitwarden understands.
# Additionally, those logins that fit will be moved to an 'Archive' folder within pass, in order to ease migrating the rest of the logins manually.
pass_to_bitwarden "$1" "$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment