freedombone-remote 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. # User to create the list for
  35. MY_USERNAME=$USER
  36. # Filename of the remote backups list
  37. FRIENDS_SERVERS_LIST=
  38. # Minimum password length in characters
  39. MINIMUM_PASSWORD_LENGTH=8
  40. # How many remote locations were specified
  41. entering_remote_backups_ctr=0
  42. # Title shown
  43. TITLE='Remote Backup'
  44. # Whether to include the capability of adding reciprocal user accounts
  45. # such that whoever is running a remote server can also use your server to
  46. # store backups
  47. RECIPROCAL="no"
  48. function show_help {
  49. echo ''
  50. echo $"${PROJECT_NAME}-remote -u [username] -l [backup list filename] -m [min password length]"
  51. echo ''
  52. echo $'Creates an inventory of remote backup locations'
  53. echo ''
  54. echo ''
  55. echo $' -h --help Show help'
  56. echo $' -u --username User to create the backups.list file for'
  57. echo $' -l --list Remote backup list (usually /home/$USER/backup.list)'
  58. echo $' -m --min Minimum password length (characters)'
  59. echo $' -r --reciprocal Whether to add reciprocal user accounts'
  60. echo $' -t --title Title shown'
  61. echo ''
  62. exit 0
  63. }
  64. # Get the commandline options
  65. while [[ $# > 1 ]]
  66. do
  67. key="$1"
  68. case $key in
  69. -h|--help)
  70. show_help
  71. ;;
  72. # backup list filename
  73. # typically /home/$USER/backup.list
  74. -l|--list)
  75. shift
  76. FRIENDS_SERVERS_LIST="$1"
  77. ;;
  78. # username within /home
  79. -u|--user)
  80. shift
  81. MY_USERNAME="$1"
  82. ;;
  83. # Minimum password length
  84. -m|--min)
  85. shift
  86. MINIMUM_PASSWORD_LENGTH="$1"
  87. ;;
  88. # Title shown
  89. -t|--title)
  90. shift
  91. TITLE="$1"
  92. ;;
  93. # reciprocal user accounts
  94. -r|--reciprocal)
  95. shift
  96. RECIPROCAL="yes"
  97. ;;
  98. *)
  99. # unknown option
  100. ;;
  101. esac
  102. shift
  103. done
  104. function interactive_configuration_remote_backups {
  105. if [ ! $MY_USERNAME ]; then
  106. echo $'Please specify a username with the -u option'
  107. exit 7356
  108. fi
  109. if [ ! /home/$MY_USERNAME ]; then
  110. echo $"The user /home/$MY_USERNAME does not exist on the system"
  111. exit 3689
  112. fi
  113. if [ ! $FRIENDS_SERVERS_LIST ]; then
  114. FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list
  115. fi
  116. # clear any existing list
  117. if [ -f $FRIENDS_SERVERS_LIST ]; then
  118. rm -f $FRIENDS_SERVERS_LIST
  119. touch $FRIENDS_SERVERS_LIST
  120. fi
  121. # number of entries made
  122. entering_remote_backups_ctr=1
  123. entering_remote_backups_done="no"
  124. remote_ssh_username=""
  125. remote_ssh_domain=""
  126. remote_ssh_port=""
  127. remote_ssh_password=""
  128. remote_ssh_reciprocal_username=""
  129. remote_ssh_reciprocal_password=""
  130. while [[ $entering_remote_backups_done == "no" ]]
  131. do
  132. data=$(tempfile 2>/dev/null)
  133. trap "rm -f $data" 0 1 2 5 15
  134. if [[ $RECIPROCAL == "yes" ]]; then
  135. dialog --backtitle "Freedombone Configuration" \
  136. --title "$TITLE ${entering_remote_backups_ctr}" \
  137. --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 \
  138. "Username:" 1 1 "$remote_ssh_username" 1 23 16 15 \
  139. "Domain:" 2 1 "$remote_ssh_domain" 2 23 16 15 \
  140. "SSH port:" 3 1 "2222" 3 23 5 4 \
  141. "Password:" 4 1 "$remote_ssh_password" 4 23 20 100 \
  142. "Reciprocal Username:" 5 1 "$remote_ssh_reciprocal_username" 5 23 20 100 \
  143. "Reciprocal Password:" 6 1 "$remote_ssh_reciprocal_password" 6 23 20 100 \
  144. 2> $data
  145. else
  146. dialog --backtitle "Freedombone Configuration" \
  147. --title "$TITLE ${entering_remote_backups_ctr}" \
  148. --form "\nPlease specify the SSH login details for the remote server" 15 50 4 \
  149. "Username:" 1 1 "$remote_ssh_username" 1 23 16 15 \
  150. "Domain:" 2 1 "$remote_ssh_domain" 2 23 16 15 \
  151. "SSH port:" 3 1 "2222" 3 23 5 4 \
  152. "Password:" 4 1 "$remote_ssh_password" 4 23 20 100 \
  153. 2> $data
  154. fi
  155. sel=$?
  156. case $sel in
  157. 1) entering_remote_backups_done="yes";;
  158. 255) entering_remote_backups_done="yes";;
  159. esac
  160. remote_ssh_username=$(cat $data | sed -n 1p)
  161. remote_ssh_domain=$(cat $data | sed -n 2p)
  162. remote_ssh_port=$(cat $data | sed -n 3p)
  163. remote_ssh_password=$(cat $data | sed -n 4p)
  164. remote_ssh_reciprocal_username=$(cat $data | sed -n 5p)
  165. remote_ssh_reciprocal_password=$(cat $data | sed -n 6p)
  166. if [[ $remote_ssh_username != "" && \
  167. $remote_ssh_domain != "" && \
  168. $remote_ssh_port != "" && \
  169. $remote_ssh_password != "" ]]; then
  170. if [ ${#remote_ssh_password} -lt $MINIMUM_PASSWORD_LENGTH ]; then
  171. dialog --title "Password quality check" --msgbox "The password given was too short. It must be at least $MINIMUM_PASSWORD_LENGTH characters" 6 40
  172. else
  173. if [[ $RECIPROCAL == "yes" ]]; then
  174. if [[ $remote_ssh_reciprocal_username != "" && \
  175. $remote_ssh_reciprocal_password != "" ]]; then
  176. if [ ${#remote_ssh_reciprocal_password} -lt $MINIMUM_PASSWORD_LENGTH ]; then
  177. dialog --title "Password quality check" --msgbox "The reciprocal password given was too short. It must be at least $MINIMUM_PASSWORD_LENGTH characters" 6 40
  178. else
  179. echo ${remote_ssh_reciprocal_username}:${remote_ssh_reciprocal_password}::::/home/${remote_ssh_reciprocal_username}:bash | newusers
  180. echo "$remote_ssh_username@$remote_ssh_domain $remote_ssh_port /home/$remote_ssh_username $remote_ssh_password" >> $FRIENDS_SERVERS_LIST
  181. remote_ssh_username=""
  182. remote_ssh_domain=""
  183. remote_ssh_port=""
  184. remote_ssh_password=""
  185. remote_ssh_reciprocal_username=""
  186. remote_ssh_reciprocal_password=""
  187. entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
  188. fi
  189. else
  190. echo "$remote_ssh_username@$remote_ssh_domain $remote_ssh_port /home/$remote_ssh_username $remote_ssh_password" >> $FRIENDS_SERVERS_LIST
  191. remote_ssh_username=""
  192. remote_ssh_domain=""
  193. remote_ssh_port=""
  194. remote_ssh_password=""
  195. remote_ssh_reciprocal_username=""
  196. remote_ssh_reciprocal_password=""
  197. entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
  198. fi
  199. else
  200. echo "$remote_ssh_username@$remote_ssh_domain $remote_ssh_port /home/$remote_ssh_username $remote_ssh_password" >> $FRIENDS_SERVERS_LIST
  201. remote_ssh_username=""
  202. remote_ssh_domain=""
  203. remote_ssh_port=""
  204. remote_ssh_password=""
  205. entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
  206. fi
  207. fi
  208. else
  209. entering_remote_backups_done="yes"
  210. fi
  211. done
  212. if [ -f $FRIENDS_SERVERS_LIST ]; then
  213. chown $MY_USERNAME:$MY_USERNAME $FRIENDS_SERVERS_LIST
  214. fi
  215. }
  216. function show_result {
  217. clear
  218. if (( $entering_remote_backups_ctr < 2 )); then
  219. echo $'No remote backup locations were specified'
  220. exit 0
  221. fi
  222. if [ ! -f $FRIENDS_SERVERS_LIST ]; then
  223. echo $"No remote backups list found: $FRIENDS_SERVERS_LIST"
  224. exit 7358
  225. fi
  226. echo ''
  227. echo $"Remote backups list: $FRIENDS_SERVERS_LIST"
  228. echo ''
  229. echo $'Contents:'
  230. echo ''
  231. cat $FRIENDS_SERVERS_LIST
  232. echo ''
  233. }
  234. if [ ! $FRIENDS_SERVERS_LIST ]; then
  235. FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list
  236. fi
  237. interactive_configuration_remote_backups
  238. show_result
  239. exit 0