freedombone-pass 9.3KB

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