freedombone-pass 8.9KB

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