freedombone-remote 9.7KB

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