freedombone-utils-config 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Initial reading of the configuration file, typically called freedombone.cfg
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
  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. configuration_variables=(FRIENDS_MIRRORS_SERVER
  31. FRIENDS_MIRRORS_SSH_PORT
  32. FRIENDS_MIRRORS_PASSWORD
  33. MY_MIRRORS_PASSWORD
  34. SYSTEM_TYPE
  35. SSL_PROTOCOLS
  36. SSL_CIPHERS
  37. SSH_CIPHERS
  38. SSH_MACS
  39. SSH_KEX
  40. SSH_HOST_KEY_ALGORITHMS
  41. SSH_PASSWORDS
  42. REFRESH_GPG_KEYS_HOURS
  43. GPG_KEYSERVER
  44. ENABLE_SOCIAL_KEY_MANAGEMENT
  45. MY_USERNAME
  46. DOMAIN_NAME
  47. DEFAULT_DOMAIN_NAME
  48. DEFAULT_DOMAIN_CODE
  49. EMAIL_DOMAIN_CODE
  50. XMPP_DOMAIN_CODE
  51. NAMESERVER1
  52. NAMESERVER2
  53. NAMESERVER3
  54. NAMESERVER4
  55. NAMESERVER5
  56. NAMESERVER6
  57. GET_IP_ADDRESS_URL
  58. DDNS_PROVIDER
  59. DDNS_USERNAME
  60. DDNS_PASSWORD
  61. LOCAL_NETWORK_STATIC_IP_ADDRESS
  62. ROUTER_IP_ADDRESS
  63. CPU_CORES
  64. WEBSERVER_LOG_LEVEL
  65. ROUTE_THROUGH_TOR
  66. MY_NAME
  67. MY_EMAIL_ADDRESS
  68. INSTALLING_ON_BBB
  69. SSH_PORT
  70. INSTALLED_WITHIN_DOCKER
  71. GPG_ENCRYPT_STORED_EMAIL
  72. MY_GPG_PUBLIC_KEY
  73. MY_GPG_PRIVATE_KEY
  74. MY_GPG_PUBLIC_KEY_ID
  75. USB_DRIVE
  76. MAX_PHP_MEMORY
  77. ONION_ONLY
  78. DEFAULT_LANGUAGE
  79. MINIMAL_INSTALL
  80. LETSENCRYPT_SERVER
  81. WIFI_INTERFACE
  82. WIFI_SSID
  83. WIFI_TYPE
  84. WIFI_PASSPHRASE
  85. WIFI_HOTSPOT
  86. WIFI_NETWORKS_FILE
  87. DEFAULT_SEARCH
  88. SEARCH_ENGINE_PASSWORD
  89. PROJECT_WEBSITE
  90. PROJECT_REPO
  91. GPGIT_REPO
  92. GPGIT_COMMIT
  93. NGINX_ENSITE_REPO
  94. NGINX_ENSITE_REPO
  95. NGINX_ENSITE_COMMIT
  96. CLEANUP_MAILDIR_COMMIT
  97. CLEANUP_MAILDIR_REPO
  98. INADYN_REPO
  99. INADYN_COMMIT
  100. DH_KEYLENGTH
  101. WIFI_CHANNEL
  102. IPV6_NETWORK
  103. HWRNG_TYPE
  104. ENABLE_BATMAN
  105. PUBLIC_MAILING_LIST)
  106. function get_completion_param {
  107. param_name="$1"
  108. if [ ${#param_name} -gt 0 ]; then
  109. if [ $COMPLETION_FILE ]; then
  110. if [ -f $COMPLETION_FILE ]; then
  111. if grep -q "${param_name}:" $COMPLETION_FILE; then
  112. param_value=$(cat $COMPLETION_FILE | grep "${param_name}:" | head -n 1 | awk -F ':' '{print $2}')
  113. echo "$param_value"
  114. return
  115. fi
  116. fi
  117. fi
  118. fi
  119. echo ""
  120. }
  121. function set_completion_param {
  122. param_name="$1"
  123. param_value="$2"
  124. if [ ! $COMPLETION_FILE ]; then
  125. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  126. fi
  127. if [ ${#COMPLETION_FILE} -eq 0 ]; then
  128. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  129. fi
  130. if [ ${#param_name} -gt 0 ]; then
  131. if [ ${#param_value} -gt 0 ]; then
  132. if [ -f $COMPLETION_FILE ]; then
  133. if grep -q "${param_name}:" $COMPLETION_FILE; then
  134. sed -i "s|${param_name}:.*|${param_name}:${param_value}|g" $COMPLETION_FILE
  135. else
  136. echo "${param_name}:${param_value}" >> $COMPLETION_FILE
  137. fi
  138. else
  139. echo "${param_name}:${param_value}" > $COMPLETION_FILE
  140. fi
  141. fi
  142. fi
  143. }
  144. function mark_completed {
  145. param_name="$1"
  146. if [ ! $COMPLETION_FILE ]; then
  147. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  148. fi
  149. if [ ${#COMPLETION_FILE} -eq 0 ]; then
  150. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  151. fi
  152. if [ ${#param_name} -gt 0 ]; then
  153. if [ -f $COMPLETION_FILE ]; then
  154. if ! grep -Fxq "${param_name}" $COMPLETION_FILE; then
  155. echo "${param_name}" >> $COMPLETION_FILE
  156. fi
  157. else
  158. echo "${param_name}" > $COMPLETION_FILE
  159. fi
  160. fi
  161. }
  162. function is_completed {
  163. param_name="$1"
  164. if [ ! $COMPLETION_FILE ]; then
  165. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  166. fi
  167. if [ ${#COMPLETION_FILE} -eq 0 ]; then
  168. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  169. fi
  170. if [ ${#param_name} -gt 0 ]; then
  171. if [ -f $COMPLETION_FILE ]; then
  172. if grep -Fxq "${param_name}" $COMPLETION_FILE; then
  173. echo "1"
  174. return
  175. fi
  176. fi
  177. fi
  178. echo "0"
  179. }
  180. function read_config_param {
  181. param_name="$1"
  182. if [ ${#param_name} -gt 0 ]; then
  183. if [ $CONFIGURATION_FILE ]; then
  184. if [ -f $CONFIGURATION_FILE ]; then
  185. if grep -q "${param_name}=" $CONFIGURATION_FILE; then
  186. param_value=$(cat $CONFIGURATION_FILE | grep "${param_name}=" | head -n 1 | awk -F '=' '{print $2}')
  187. if [[ "${param_value}" != *" "* ]]; then
  188. export ${param_name}=${param_value}
  189. else
  190. export ${param_name}="${param_value}"
  191. fi
  192. fi
  193. fi
  194. fi
  195. fi
  196. }
  197. function config_param_exists {
  198. param_name="$1"
  199. if [ ${#param_name} -gt 0 ]; then
  200. if [ $CONFIGURATION_FILE ]; then
  201. if [ -f $CONFIGURATION_FILE ]; then
  202. if grep -q "${param_name}=" $CONFIGURATION_FILE; then
  203. param_value=$(cat $CONFIGURATION_FILE | grep "${param_name}=" | head -n 1 | awk -F '=' '{print $2}')
  204. if [ ${#param_value} -gt 0 ]; then
  205. echo "1"
  206. return
  207. fi
  208. fi
  209. fi
  210. fi
  211. fi
  212. echo "0"
  213. }
  214. function write_config_param {
  215. param_name="$1"
  216. param_value="$2"
  217. if [ ! $CONFIGURATION_FILE ]; then
  218. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  219. fi
  220. if [ ${#CONFIGURATION_FILE} -eq 0 ]; then
  221. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  222. fi
  223. if [ ${#param_name} -gt 0 ]; then
  224. if [ ${#param_value} -gt 0 ]; then
  225. if [ -f $CONFIGURATION_FILE ]; then
  226. if grep -q "${param_name}=" $CONFIGURATION_FILE; then
  227. sed -i "s|${param_name}=.*|${param_name}=${param_value}|g" $CONFIGURATION_FILE
  228. else
  229. echo "${param_name}=${param_value}" >> $CONFIGURATION_FILE
  230. fi
  231. else
  232. echo "${param_name}=${param_value}" > $CONFIGURATION_FILE
  233. fi
  234. fi
  235. fi
  236. }
  237. function remove_config_param {
  238. param_name="$1"
  239. if [ ${#param_name} -gt 0 ]; then
  240. if [ $CONFIGURATION_FILE ]; then
  241. if [ -f $CONFIGURATION_FILE ]; then
  242. if grep -q "${param_name}=" $CONFIGURATION_FILE; then
  243. sed -i "/${param_name}=/d" $CONFIGURATION_FILE
  244. fi
  245. fi
  246. fi
  247. fi
  248. }
  249. function remove_completion_param {
  250. param_name="$1"
  251. if [ ${#param_name} -gt 0 ]; then
  252. if [ $COMPLETION_FILE ]; then
  253. if [ -f $COMPLETION_FILE ]; then
  254. if grep -Fxq "${param_name}" $COMPLETION_FILE; then
  255. sed -i "/${param_name}/d" $COMPLETION_FILE
  256. fi
  257. if grep -q "${param_name}:" $COMPLETION_FILE; then
  258. sed -i "/${param_name}:/d" $COMPLETION_FILE
  259. fi
  260. fi
  261. fi
  262. fi
  263. }
  264. function save_configuration_values {
  265. for v in "${configuration_variables[@]}"
  266. do
  267. write_config_param "$v" "${!v}"
  268. done
  269. }
  270. function read_configuration_values {
  271. # if not installing on a Beaglebone then use sdb as the USB drive by default
  272. if [ ! $INSTALLING_ON_BBB ]; then
  273. if [[ $USB_DRIVE == /dev/sda1 ]]; then
  274. USB_DRIVE=/dev/sdb1
  275. fi
  276. fi
  277. if [ -f $CONFIGURATION_FILE ]; then
  278. read_repo_servers
  279. # Ensure that a copy of the config exists for upgrade purposes
  280. if [[ $CONFIGURATION_FILE != "/root/${PROJECT_NAME}.cfg" ]]; then
  281. cp $CONFIGURATION_FILE /root/${PROJECT_NAME}.cfg
  282. fi
  283. for v in "${configuration_variables[@]}"
  284. do
  285. read_config_param "$v"
  286. done
  287. if grep -q "DEBIAN_REPO" $CONFIGURATION_FILE; then
  288. read_config_param "DEBIAN_REPO"
  289. CHECK_MESSAGE=$"Check your internet connection, /etc/network/interfaces and /etc/resolv.conf, then delete $COMPLETION_FILE, run 'rm -fR /var/lib/apt/lists/* && apt-get update --fix-missing' and run this script again. If hash sum mismatches persist then try setting $DEBIAN_REPO to a different mirror and also change /etc/apt/sources.list."
  290. fi
  291. fi
  292. }
  293. function read_configuration {
  294. if [[ $INSTALLING_FROM_CONFIGURATION_FILE == "yes" ]]; then
  295. if [ ! -f $CONFIGURATION_FILE ]; then
  296. echo $"The configuration file $CONFIGURATION_FILE was not found"
  297. exit 8935
  298. fi
  299. fi
  300. read_configuration_values
  301. }
  302. function check_system_type {
  303. if [ ${#SYSTEM_TYPE} -eq 0 ]; then
  304. echo $'Unknown system type'
  305. exit 763572
  306. fi
  307. echo "System type: $SYSTEM_TYPE"
  308. }
  309. # check that domain names are valid and that they are unique for each app
  310. function check_domains {
  311. specified_domains=$(cat $CONFIGURATION_FILE | grep "_DOMAIN_NAME" | uniq)
  312. domains_array=($specified_domains)
  313. checked_domains=()
  314. for d in "${domains_array[@]}"
  315. do
  316. domain_param=$(echo "$d" | awk -F '=' '{print $1}')
  317. if [[ "$domain_param" != "DEFAULT_DOMAIN_NAME" ]]; then
  318. domain_value=$(echo "$d" | awk -F '=' '{print $2}')
  319. if [[ "$domain_value" != "${PROJECT_NAME}.local" ]]; then
  320. item_in_array "${domain_value}" "${checked_domains[@]}"
  321. if [[ $? != 0 ]]; then
  322. # test that this is a valid domain name
  323. function_check test_domain_name
  324. test_domain_name "$domain_value"
  325. # add it to the list of domains
  326. checked_domains+=("$domain_value")
  327. else
  328. echo $"Domain ${domain_value} collides with another app. The domain for each app should be unique."
  329. exit 673925
  330. fi
  331. fi
  332. fi
  333. done
  334. }
  335. # NOTE: deliberately no exit 0