freedombone-remote 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Creates an inventory of remote backup locations
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. NO_OF_ARGS=$#
  31. PROJECT_NAME='freedombone'
  32. export TEXTDOMAIN=${PROJECT_NAME}-remote
  33. export TEXTDOMAINDIR="/usr/share/locale"
  34. CONFIG_FILE=$HOME/${PROJECT_NAME}.cfg
  35. # User to create the list for
  36. MY_USERNAME=$USER
  37. # Filename of the remote backups list
  38. FRIENDS_SERVERS_LIST=
  39. # Minimum password length in characters
  40. MINIMUM_PASSWORD_LENGTH=10
  41. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  42. MINIMUM_PASSWORD_LENGTH=$(cat /usr/local/bin/${PROJECT_NAME}-utils-passwords | grep 'MINIMUM_PASSWORD_LENGTH=' | head -n 1 | awk -F '=' '{print $2}')
  43. else
  44. if [ -f /usr/bin/${PROJECT_NAME} ]; then
  45. MINIMUM_PASSWORD_LENGTH=$(cat /usr/bin/${PROJECT_NAME}-utils-passwords | grep 'MINIMUM_PASSWORD_LENGTH=' | head -n 1 | awk -F '=' '{print $2}')
  46. fi
  47. fi
  48. # How many remote locations were specified
  49. entering_remote_backups_ctr=0
  50. # Title shown
  51. TITLE='Remote Backup'
  52. # Whether to include the capability of adding reciprocal user accounts
  53. # such that whoever is running a remote server can also use your server to
  54. # store backups
  55. RECIPROCAL="no"
  56. function show_help {
  57. echo ''
  58. echo $"${PROJECT_NAME}-remote -u [username] -l [backup list filename] -m [min password length]"
  59. echo ''
  60. echo $'Creates an inventory of remote backup locations'
  61. echo ''
  62. echo ''
  63. echo $' -h --help Show help'
  64. echo $' -u --username User to create the backups.list file for'
  65. echo $' -l --list Remote backup list (usually /home/$USER/backup.list)'
  66. echo $' -m --min Minimum password length (characters)'
  67. echo $' -r --reciprocal Whether to add reciprocal user accounts'
  68. echo $' -t --title Title shown'
  69. echo ''
  70. exit 0
  71. }
  72. # Get the commandline options
  73. while [[ $# > 1 ]]
  74. do
  75. key="$1"
  76. case $key in
  77. -h|--help)
  78. show_help
  79. ;;
  80. # backup list filename
  81. # typically /home/$USER/backup.list
  82. -l|--list)
  83. shift
  84. FRIENDS_SERVERS_LIST="$1"
  85. ;;
  86. # username within /home
  87. -u|--user)
  88. shift
  89. MY_USERNAME="$1"
  90. ;;
  91. # Minimum password length
  92. -m|--min)
  93. shift
  94. MINIMUM_PASSWORD_LENGTH="$1"
  95. ;;
  96. # Title shown
  97. -t|--title)
  98. shift
  99. TITLE="$1"
  100. ;;
  101. # reciprocal user accounts
  102. -r|--reciprocal)
  103. shift
  104. RECIPROCAL="yes"
  105. ;;
  106. *)
  107. # unknown option
  108. ;;
  109. esac
  110. shift
  111. done
  112. function interactive_configuration_remote_backups {
  113. if [ ! $MY_USERNAME ]; then
  114. echo $'Please specify a username with the -u option'
  115. exit 7356
  116. fi
  117. if [ ! /home/$MY_USERNAME ]; then
  118. echo $"The user /home/$MY_USERNAME does not exist on the system"
  119. exit 3689
  120. fi
  121. if [ ! $FRIENDS_SERVERS_LIST ]; then
  122. FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list
  123. fi
  124. # clear any existing list
  125. if [ -f $FRIENDS_SERVERS_LIST ]; then
  126. rm -f $FRIENDS_SERVERS_LIST
  127. touch $FRIENDS_SERVERS_LIST
  128. fi
  129. # number of entries made
  130. entering_remote_backups_ctr=1
  131. entering_remote_backups_done="no"
  132. remote_ssh_username=""
  133. remote_ssh_domain=""
  134. remote_ssh_port=""
  135. remote_ssh_password=""
  136. remote_ssh_reciprocal_username=""
  137. remote_ssh_reciprocal_password=""
  138. while [[ $entering_remote_backups_done == "no" ]]
  139. do
  140. data=$(tempfile 2>/dev/null)
  141. trap "rm -f $data" 0 1 2 5 15
  142. if [[ $RECIPROCAL == "yes" ]]; then
  143. dialog --backtitle "Freedombone Configuration" \
  144. --title "$TITLE ${entering_remote_backups_ctr}" \
  145. --form "\nPlease specify the SSH login details for the remote server\n\nThe reciprocal entries are optional, and can be used if you wish to set up a user account on this system for whoever runs the remote server to also use for backups" 20 50 8 \
  146. "Username:" 1 1 "$remote_ssh_username" 1 23 16 15 \
  147. "Domain:" 2 1 "$remote_ssh_domain" 2 23 16 15 \
  148. "SSH port:" 3 1 "2222" 3 23 5 4 \
  149. "Password:" 4 1 "$remote_ssh_password" 4 23 20 100 \
  150. "Reciprocal Username:" 5 1 "$remote_ssh_reciprocal_username" 5 23 20 100 \
  151. "Reciprocal Password:" 6 1 "$remote_ssh_reciprocal_password" 6 23 20 100 \
  152. 2> $data
  153. else
  154. dialog --backtitle "Freedombone Configuration" \
  155. --title "$TITLE ${entering_remote_backups_ctr}" \
  156. --form "\nPlease specify the SSH login details for the remote server" 15 50 4 \
  157. "Username:" 1 1 "$remote_ssh_username" 1 23 16 15 \
  158. "Domain:" 2 1 "$remote_ssh_domain" 2 23 16 15 \
  159. "SSH port:" 3 1 "2222" 3 23 5 4 \
  160. "Password:" 4 1 "$remote_ssh_password" 4 23 20 100 \
  161. 2> $data
  162. fi
  163. sel=$?
  164. case $sel in
  165. 1) entering_remote_backups_done="yes";;
  166. 255) entering_remote_backups_done="yes";;
  167. esac
  168. remote_ssh_username=$(cat $data | sed -n 1p)
  169. remote_ssh_domain=$(cat $data | sed -n 2p)
  170. remote_ssh_port=$(cat $data | sed -n 3p)
  171. remote_ssh_password=$(cat $data | sed -n 4p)
  172. remote_ssh_reciprocal_username=$(cat $data | sed -n 5p)
  173. remote_ssh_reciprocal_password=$(cat $data | sed -n 6p)
  174. if [[ $remote_ssh_username != "" && \
  175. $remote_ssh_domain != "" && \
  176. $remote_ssh_port != "" && \
  177. $remote_ssh_password != "" ]]; then
  178. if [ ${#remote_ssh_password} -lt $MINIMUM_PASSWORD_LENGTH ]; then
  179. dialog --title "Password quality check" --msgbox "The password given was too short. It must be at least $MINIMUM_PASSWORD_LENGTH characters" 6 40
  180. else
  181. if [[ $RECIPROCAL == "yes" ]]; then
  182. if [[ $remote_ssh_reciprocal_username != "" && \
  183. $remote_ssh_reciprocal_password != "" ]]; then
  184. if [ ${#remote_ssh_reciprocal_password} -lt $MINIMUM_PASSWORD_LENGTH ]; then
  185. dialog --title "Password quality check" --msgbox "The reciprocal password given was too short. It must be at least $MINIMUM_PASSWORD_LENGTH characters" 6 40
  186. else
  187. echo ${remote_ssh_reciprocal_username}:${remote_ssh_reciprocal_password}::::/home/${remote_ssh_reciprocal_username}:bash | newusers
  188. echo "$remote_ssh_username@$remote_ssh_domain $remote_ssh_port /home/$remote_ssh_username $remote_ssh_password" >> $FRIENDS_SERVERS_LIST
  189. remote_ssh_username=""
  190. remote_ssh_domain=""
  191. remote_ssh_port=""
  192. remote_ssh_password=""
  193. remote_ssh_reciprocal_username=""
  194. remote_ssh_reciprocal_password=""
  195. entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
  196. fi
  197. else
  198. echo "$remote_ssh_username@$remote_ssh_domain $remote_ssh_port /home/$remote_ssh_username $remote_ssh_password" >> $FRIENDS_SERVERS_LIST
  199. remote_ssh_username=""
  200. remote_ssh_domain=""
  201. remote_ssh_port=""
  202. remote_ssh_password=""
  203. remote_ssh_reciprocal_username=""
  204. remote_ssh_reciprocal_password=""
  205. entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
  206. fi
  207. else
  208. echo "$remote_ssh_username@$remote_ssh_domain $remote_ssh_port /home/$remote_ssh_username $remote_ssh_password" >> $FRIENDS_SERVERS_LIST
  209. remote_ssh_username=""
  210. remote_ssh_domain=""
  211. remote_ssh_port=""
  212. remote_ssh_password=""
  213. entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
  214. fi
  215. fi
  216. else
  217. entering_remote_backups_done="yes"
  218. fi
  219. done
  220. if [ -f $FRIENDS_SERVERS_LIST ]; then
  221. chown $MY_USERNAME:$MY_USERNAME $FRIENDS_SERVERS_LIST
  222. fi
  223. }
  224. function show_result {
  225. clear
  226. if (( $entering_remote_backups_ctr < 2 )); then
  227. echo $'No remote backup locations were specified'
  228. exit 0
  229. fi
  230. if [ ! -f $FRIENDS_SERVERS_LIST ]; then
  231. echo $"No remote backups list found: $FRIENDS_SERVERS_LIST"
  232. exit 7358
  233. fi
  234. echo ''
  235. echo $"Remote backups list: $FRIENDS_SERVERS_LIST"
  236. echo ''
  237. echo $'Contents:'
  238. echo ''
  239. cat $FRIENDS_SERVERS_LIST
  240. echo ''
  241. }
  242. if [ ! $FRIENDS_SERVERS_LIST ]; then
  243. FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list
  244. fi
  245. interactive_configuration_remote_backups
  246. show_result
  247. exit 0