freedombone-pass 9.5KB

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