freedombone-pass 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # It's useful to be able to store user passwords, but not a good
  12. # idea to do that in plain text. This implements a simple password
  13. # store. It gpg symmetric encrypts passwords using the backups
  14. # private key as the passphrase.
  15. #
  16. # In order for an adversary to obtain the passwords they must have
  17. # the backups GPG key, which is not obtainable from local or remote
  18. # backups and can only happen if they get root access to the system
  19. # (in which case it's game over anyhow) or if they can decrypt
  20. # a master keydrive or obtain sufficient keydrive fragments.
  21. #
  22. # License
  23. # =======
  24. #
  25. # Copyright (C) 2016 Bob Mottram <bob@freedombone.net>
  26. #
  27. # This program is free software: you can redistribute it and/or modify
  28. # it under the terms of the GNU Affero General Public License as published by
  29. # the Free Software Foundation, either version 3 of the License, or
  30. # (at your option) any later version.
  31. #
  32. # This program is distributed in the hope that it will be useful,
  33. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. # GNU Affero General Public License for more details.
  36. #
  37. # You should have received a copy of the GNU Affero General Public License
  38. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  39. PROJECT_NAME='freedombone'
  40. export TEXTDOMAIN=${PROJECT_NAME}-pass
  41. export TEXTDOMAINDIR="/usr/share/locale"
  42. MY_BACKUP_KEY_ID=
  43. CURR_USERNAME=
  44. REMOVE_USERNAME=
  45. CURR_APP=
  46. REMOVE_APP=
  47. CURR_PASSWORD=""
  48. function get_backup_key_id {
  49. MY_BACKUP_KEY_ID=$(gpg --list-keys "(backup key)" | \
  50. grep 'pub ' | awk -F ' ' '{print $2}' | \
  51. awk -F '/' '{print $2}')
  52. if [ ${#MY_BACKUP_KEY_ID} -lt 4 ]; then
  53. echo $"Error: gpg backup key was not found"
  54. return 58213
  55. fi
  56. }
  57. function pass_show_help {
  58. echo ''
  59. echo $"${PROJECT_NAME}-pass"
  60. echo ''
  61. echo $'Password store using gpg'
  62. echo ''
  63. echo $' -h --help Show help'
  64. echo $' -u --user [name] Username'
  65. echo $' -a --app [name] Name of the application'
  66. echo $' -p --pass [password] The password to store'
  67. echo ''
  68. echo $'To encrypt a password:'
  69. echo ''
  70. echo $" ${PROJECT_NAME}-pass -u [username] -a [app] -p [password]"
  71. echo ''
  72. echo $'To retrieve a password:'
  73. echo $''
  74. echo $" ${PROJECT_NAME}-pass -u [username] -a [app]"
  75. echo ''
  76. echo $'To remove passwords for a user:'
  77. echo $''
  78. echo $" ${PROJECT_NAME}-pass -r [username]"
  79. echo ''
  80. echo $'To remove an application password for a user:'
  81. echo $''
  82. echo $" ${PROJECT_NAME}-pass --u [username] --rmapp [name]"
  83. echo ''
  84. exit 0
  85. }
  86. function pad_string {
  87. echo -n -e "$1" | sed -e :a -e 's/^.\{1,128\}$/& /;ta'
  88. }
  89. while [[ $# > 1 ]]
  90. do
  91. key="$1"
  92. case $key in
  93. -h|--help)
  94. pass_show_help
  95. ;;
  96. -u|--user|--username)
  97. shift
  98. CURR_USERNAME="${1}"
  99. ;;
  100. -r|--rm|--remove)
  101. shift
  102. REMOVE_USERNAME="${1}"
  103. ;;
  104. --rmapp|--removeapp)
  105. shift
  106. REMOVE_APP="${1}"
  107. ;;
  108. -a|--app|--application)
  109. shift
  110. CURR_APP="${1}"
  111. ;;
  112. -p|--pass|--password|--passphrase)
  113. shift
  114. CURR_PASSWORD="${1}"
  115. ;;
  116. *)
  117. # unknown option
  118. ;;
  119. esac
  120. shift
  121. done
  122. if [ ${REMOVE_USERNAME} ]; then
  123. if [ -d ~/.passwords/${REMOVE_USERNAME} ]; then
  124. rm -rf ~/.passwords/${REMOVE_USERNAME}
  125. fi
  126. exit 0
  127. fi
  128. get_backup_key_id
  129. # Use the backups private key as a symmetric passphrase
  130. MASTER_PASSWORD=$(gpg -q --armor --export-secret-key $MY_BACKUP_KEY_ID | sed '/---/d' | sed '/Version/d' | sed '/^$/d')
  131. if [ ! $CURR_USERNAME ]; then
  132. echo $'Error: No username given'
  133. exit 1
  134. fi
  135. if [ ! -d /home/$CURR_USERNAME ]; then
  136. echo $"Error: User $CURR_USERNAME does not exist"
  137. exit 2
  138. fi
  139. if [ ${REMOVE_APP} ]; then
  140. if [ -d ~/.passwords/${CURR_USERNAME}/${REMOVE_APP} ]; then
  141. shred -zu ~/.passwords/${CURR_USERNAME}/${REMOVE_APP}
  142. fi
  143. exit 0
  144. fi
  145. if [ ! $CURR_APP ]; then
  146. echo $'Error: No app name given'
  147. exit 3
  148. fi
  149. if [ ${#CURR_PASSWORD} -eq 0 ]; then
  150. # retrieve password
  151. if [ ! -f ~/.passwords/$CURR_USERNAME/$CURR_APP ]; then
  152. echo ""
  153. exit 4
  154. else
  155. pass=$(gpg -dq --passphrase "$MASTER_PASSWORD" ~/.passwords/$CURR_USERNAME/$CURR_APP)
  156. echo "${pass}" | xargs
  157. fi
  158. else
  159. # store password
  160. if [ ! -d ~/.passwords/$CURR_USERNAME ]; then
  161. mkdir -p ~/.passwords/$CURR_USERNAME
  162. fi
  163. # padding helps to ensure than nothing can be learned from the length of the cyphertext
  164. pad_string "${CURR_PASSWORD}" | gpg -ca --cipher-algo AES256 --passphrase "$MASTER_PASSWORD" > ~/.passwords/$CURR_USERNAME/$CURR_APP
  165. if [ ! -f ~/.passwords/$CURR_USERNAME/$CURR_APP ]; then
  166. exit 5
  167. fi
  168. fi
  169. exit 0