freedombone-pass 9.5KB

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