freedombone-pass 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. TESTS=
  49. # If this file is present then don't store passwords
  50. NO_PASSWORD_STORE_FILE=~/.nostore
  51. function get_backup_key_id {
  52. MY_BACKUP_KEY_ID=$(gpg --list-keys "(backup key)" | \
  53. grep 'pub ' | awk -F ' ' '{print $2}' | \
  54. awk -F '/' '{print $2}')
  55. if [ ${#MY_BACKUP_KEY_ID} -lt 4 ]; then
  56. echo $"Error: gpg backup key was not found"
  57. return 58213
  58. fi
  59. }
  60. function pass_show_help {
  61. echo ''
  62. echo $"${PROJECT_NAME}-pass"
  63. echo ''
  64. echo $'Password store using gpg'
  65. echo ''
  66. echo $' -h --help Show help'
  67. echo $' -u --user [name] Username'
  68. echo $' -a --app [name] Name of the application'
  69. echo $' -p --pass [password] The password to store'
  70. echo ''
  71. echo $'To encrypt a password:'
  72. echo ''
  73. echo $" ${PROJECT_NAME}-pass -u [username] -a [app] -p [password]"
  74. echo ''
  75. echo $'To retrieve a password:'
  76. echo $''
  77. echo $" ${PROJECT_NAME}-pass -u [username] -a [app]"
  78. echo ''
  79. echo $'To remove passwords for a user:'
  80. echo $''
  81. echo $" ${PROJECT_NAME}-pass -r [username]"
  82. echo ''
  83. echo $'To remove an application password for a user:'
  84. echo $''
  85. echo $" ${PROJECT_NAME}-pass --u [username] --rmapp [name]"
  86. echo ''
  87. exit 0
  88. }
  89. function pad_string {
  90. pass_string="$1"
  91. str_length=${#pass_string}
  92. total_padding=$((128 - str_length))
  93. leading_padding=$((1 + RANDOM % $total_padding))
  94. trailing_padding=$((total_padding - leading_padding))
  95. leading=$(printf "%-${leading_padding}s")
  96. trailing=$(printf "%-${trailing_padding}s")
  97. echo "${leading}${pass_string}${trailing}"
  98. }
  99. function remove_padding {
  100. padded_string="$1"
  101. echo -e "${padded_string}" | tr -d '[:space:]'
  102. }
  103. function run_tests {
  104. pass="SuperSecretPassword"
  105. padded=$(pad_string "$pass")
  106. if [ ${#padded} -ne 128 ]; then
  107. echo $'Incorrect padded length'
  108. exit 78352
  109. fi
  110. ${PROJECT_NAME}-pass -u root -a tests -p "$pass"
  111. returned_pass=$(${PROJECT_NAME}-pass -u root -a tests)
  112. if [[ "$pass" != "$returned_pass" ]]; then
  113. echo "pass :${pass}:"
  114. echo "padded :${padded}:"
  115. echo "returned :${returned_pass}:"
  116. exit 73825
  117. fi
  118. ${PROJECT_NAME}-pass -u root --rmapp tests
  119. echo "Tests passed"
  120. }
  121. function clear_passwords {
  122. # remove all passwords except for the root one, which is needed
  123. # for automatic database backups
  124. for d in /root/.passwords/*/ ; do
  125. USERNAME=$(echo "$d" | awk -F '/' '{print $4}')
  126. if [[ "$USERNAME" != 'root' ]]; then
  127. shred -zu /root/.passwords/$USERNAME/*
  128. rm -rf /root/.passwords/$USERNAME
  129. fi
  130. done
  131. if [ ! -f $NO_PASSWORD_STORE_FILE ]; then
  132. touch $NO_PASSWORD_STORE_FILE
  133. fi
  134. echo $'Passwords cleared. Future passwords will not be stored.'
  135. exit 0
  136. }
  137. while [[ $# > 1 ]]
  138. do
  139. key="$1"
  140. case $key in
  141. -h|--help)
  142. pass_show_help
  143. ;;
  144. -t|--test)
  145. shift
  146. TESTS=1
  147. ;;
  148. -c|--clear|--erase)
  149. clear_passwords
  150. ;;
  151. -e|--enable)
  152. shift
  153. if [ -f $NO_PASSWORD_STORE_FILE ]; then
  154. rm $NO_PASSWORD_STORE_FILE
  155. echo $'Password storage has been enabled'
  156. fi
  157. ;;
  158. -u|--user|--username)
  159. shift
  160. CURR_USERNAME="${1}"
  161. ;;
  162. -r|--rm|--remove)
  163. shift
  164. REMOVE_USERNAME="${1}"
  165. ;;
  166. --rmapp|--removeapp)
  167. shift
  168. REMOVE_APP="${1}"
  169. ;;
  170. -a|--app|--application)
  171. shift
  172. CURR_APP="${1}"
  173. ;;
  174. -p|--pass|--password|--passphrase)
  175. shift
  176. CURR_PASSWORD="${1}"
  177. ;;
  178. *)
  179. # unknown option
  180. ;;
  181. esac
  182. shift
  183. done
  184. if [ ${REMOVE_USERNAME} ]; then
  185. if [ -d ~/.passwords/${REMOVE_USERNAME} ]; then
  186. rm -rf ~/.passwords/${REMOVE_USERNAME}
  187. fi
  188. exit 0
  189. fi
  190. get_backup_key_id
  191. # Use the backups private key as a symmetric passphrase
  192. MASTER_PASSWORD=$(gpg -q --armor --export-secret-key $MY_BACKUP_KEY_ID | sed '/---/d' | sed '/Version/d' | sed '/^$/d')
  193. if [ $TESTS ]; then
  194. run_tests
  195. exit 0
  196. fi
  197. if [ ! $CURR_USERNAME ]; then
  198. echo $'Error: No username given'
  199. exit 1
  200. fi
  201. if [ ! -d /home/$CURR_USERNAME ]; then
  202. if [[ "$CURR_USERNAME" != "root" ]]; then
  203. echo $"Error: User $CURR_USERNAME does not exist"
  204. exit 2
  205. fi
  206. fi
  207. if [ ${REMOVE_APP} ]; then
  208. if [ -d ~/.passwords/${CURR_USERNAME}/${REMOVE_APP} ]; then
  209. shred -zu ~/.passwords/${CURR_USERNAME}/${REMOVE_APP}
  210. fi
  211. exit 0
  212. fi
  213. if [ ! $CURR_APP ]; then
  214. echo $'Error: No app name given'
  215. exit 3
  216. fi
  217. if [ ${#CURR_PASSWORD} -eq 0 ]; then
  218. # retrieve password
  219. if [ ! -f ~/.passwords/$CURR_USERNAME/$CURR_APP ]; then
  220. echo ""
  221. exit 4
  222. else
  223. pass=$(gpg -dq --passphrase "$MASTER_PASSWORD" ~/.passwords/$CURR_USERNAME/$CURR_APP)
  224. remove_padding "${pass}"
  225. fi
  226. else
  227. # store password
  228. if [ -f $NO_PASSWORD_STORE_FILE ]; then
  229. if [[ "$CURR_USERNAME" != 'root' ]]; then
  230. exit 0
  231. fi
  232. fi
  233. if [ ! -d ~/.passwords/$CURR_USERNAME ]; then
  234. mkdir -p ~/.passwords/$CURR_USERNAME
  235. fi
  236. # padding helps to ensure than nothing can be learned from the length of the cyphertext
  237. pad_string "${CURR_PASSWORD}" | gpg -ca --cipher-algo AES256 --passphrase "$MASTER_PASSWORD" > ~/.passwords/$CURR_USERNAME/$CURR_APP
  238. if [ ! -f ~/.passwords/$CURR_USERNAME/$CURR_APP ]; then
  239. exit 5
  240. fi
  241. fi
  242. exit 0