#!/bin/bash # # Purpose : Keep secrets in your login keychain. Source this file; the # scripts in system/ do too. # # Items the security tool makes can be read back by it without asking, so # background jobs (backups) can use them while you're logged in. # keychain_load keychain_load() { security find-generic-password -s "$1" -a "$2" -w 2> /dev/null } # keychain_save # Saved through security's interactive mode, so the secret isn't in the # process list (printf is a shell builtin). That mode splits on spaces and # reads quotes, so only secrets without them are taken: Nextcloud's app # passwords and the passphrases this makes. keychain_save() { if [[ ! "$3" =~ ^[A-Za-z0-9+/=._-]+$ ]]; then echo "That secret has characters the keychain helper can't save." >&2 return 1 fi printf 'add-generic-password -U -s "%s" -a "%s" -l "%s" -w "%s"\n' "$1" "$2" "$1 ($2)" "$3" \ | security -i > /dev/null 2>&1 \ && [ "$(keychain_load "$1" "$2")" = "$3" ] } # keychain_delete keychain_delete() { security delete-generic-password -s "$1" -a "$2" > /dev/null 2>&1 }