freedombone-utils-web 46KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Web related functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2017 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. # default search engine for command line browser
  31. DEFAULT_SEARCH='https://searx.laquadrature.net'
  32. # onion port for the default domain
  33. DEFAULT_DOMAIN_ONION_PORT=8099
  34. # Whether Let's Encrypt is enabled for all sites
  35. LETSENCRYPT_ENABLED="yes"
  36. LETSENCRYPT_SERVER='https://acme-v01.api.letsencrypt.org/directory'
  37. # list of encryption protocols
  38. SSL_PROTOCOLS="TLSv1 TLSv1.1 TLSv1.2"
  39. # Mozilla recommended default ciphers. These work better on Android
  40. # See https://wiki.mozilla.org/Security/Server_Side_TLS
  41. SSL_CIPHERS="ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS"
  42. # some mobile apps (eg. NextCloud) have not very good cipher compatibility.
  43. # These ciphers can be used for those cases
  44. SSL_CIPHERS_MOBILE="ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA"
  45. NGINX_ENSITE_REPO="https://github.com/perusio/nginx_ensite"
  46. NGINX_ENSITE_COMMIT='fa4d72ce1c0a490442c8474e9c8dc21ed52c93d0'
  47. # memory limit for php in MB
  48. MAX_PHP_MEMORY=64
  49. # logging level for Nginx
  50. WEBSERVER_LOG_LEVEL='warn'
  51. # test a domain name to see if it's valid
  52. function validate_domain_name {
  53. # count the number of dots in the domain name
  54. dots=${TEST_DOMAIN_NAME//[^.]}
  55. no_of_dots=${#dots}
  56. if (( $no_of_dots > 3 )); then
  57. TEST_DOMAIN_NAME=$"The domain $TEST_DOMAIN_NAME has too many subdomains. It should be of the type w.x.y.z, x.y.z or y.z"
  58. fi
  59. if (( $no_of_dots == 0 )); then
  60. TEST_DOMAIN_NAME=$"The domain $TEST_DOMAIN_NAME has no top level domain. It should be of the type w.x.y.z, x.y.z or y.z"
  61. fi
  62. }
  63. function nginx_disable_sniffing {
  64. domain_name=$1
  65. filename=/etc/nginx/sites-available/$domain_name
  66. echo ' add_header X-Frame-Options DENY;' >> $filename
  67. echo ' add_header X-Content-Type-Options nosniff;' >> $filename
  68. echo '' >> $filename
  69. }
  70. function nginx_limits {
  71. domain_name=$1
  72. max_body='20m'
  73. if [ $2 ]; then
  74. max_body=$2
  75. fi
  76. filename=/etc/nginx/sites-available/$domain_name
  77. echo " client_max_body_size ${max_body};" >> $filename
  78. echo ' client_body_buffer_size 128k;' >> $filename
  79. echo '' >> $filename
  80. echo ' limit_conn conn_limit_per_ip 10;' >> $filename
  81. echo ' limit_req zone=req_limit_per_ip burst=10 nodelay;' >> $filename
  82. echo '' >> $filename
  83. }
  84. function nginx_stapling {
  85. domain_name=$1
  86. filename=/etc/nginx/sites-available/$domain_name
  87. echo " ssl_stapling on;" >> $filename
  88. echo ' ssl_stapling_verify on;' >> $filename
  89. echo ' ssl_trusted_certificate /etc/ssl/certs/${domain_name}.pem;' >> $filename
  90. echo '' >> $filename
  91. }
  92. function nginx_http_redirect {
  93. # redirect port 80 to https
  94. domain_name=$1
  95. filename=/etc/nginx/sites-available/$domain_name
  96. echo 'server {' > $filename
  97. echo ' listen 80;' >> $filename
  98. echo ' listen [::]:80;' >> $filename
  99. echo " server_name ${domain_name};" >> $filename
  100. echo " root /var/www/${domain_name}/htdocs;" >> $filename
  101. echo ' access_log /dev/null;' >> $filename
  102. echo " error_log /dev/null;" >> $filename
  103. function_check nginx_limits
  104. nginx_limits $domain_name
  105. if [ ${#2} -gt 0 ]; then
  106. echo " $2;" >> $filename
  107. fi
  108. echo ' rewrite ^ https://$server_name$request_uri? permanent;' >> $filename
  109. echo '}' >> $filename
  110. echo '' >> $filename
  111. }
  112. function nginx_compress {
  113. domain_name=$1
  114. filename=/etc/nginx/sites-available/$domain_name
  115. echo ' gzip on;' >> $filename
  116. echo ' gzip_min_length 1000;' >> $filename
  117. echo ' gzip_proxied expired no-cache no-store private auth;' >> $filename
  118. echo ' gzip_types text/plain application/xml;' >> $filename
  119. }
  120. function nginx_ssl {
  121. # creates the SSL/TLS section for a website
  122. domain_name=$1
  123. mobile_ciphers=$2
  124. filename=/etc/nginx/sites-available/$domain_name
  125. echo ' ssl_stapling off;' >> $filename
  126. echo ' ssl_stapling_verify off;' >> $filename
  127. echo ' ssl on;' >> $filename
  128. echo " ssl_certificate /etc/letsencrypt/live/${domain_name}/fullchain.pem;" >> $filename
  129. echo " ssl_certificate_key /etc/letsencrypt/live/${domain_name}/privkey.pem;" >> $filename
  130. echo " ssl_dhparam /etc/ssl/certs/${domain_name}.dhparam;" >> $filename
  131. echo '' >> $filename
  132. echo ' ssl_session_cache builtin:1000 shared:SSL:10m;' >> $filename
  133. echo ' ssl_session_timeout 60m;' >> $filename
  134. echo ' ssl_prefer_server_ciphers on;' >> $filename
  135. echo " ssl_protocols $SSL_PROTOCOLS;" >> $filename
  136. if [ $mobile_ciphers ]; then
  137. echo " # Mobile compatible ciphers" >> $filename
  138. echo " ssl_ciphers '$SSL_CIPHERS_MOBILE';" >> $filename
  139. else
  140. echo " ssl_ciphers '$SSL_CIPHERS';" >> $filename
  141. fi
  142. echo " add_header Content-Security-Policy \"default-src https:; script-src https: 'unsafe-inline'; style-src https: 'unsafe-inline'\";" >> $filename
  143. echo ' add_header X-XSS-Protection "1; mode=block";' >> $filename
  144. echo ' add_header X-Robots-Tag none;' >> $filename
  145. echo ' add_header X-Download-Options noopen;' >> $filename
  146. echo ' add_header X-Permitted-Cross-Domain-Policies none;' >> $filename
  147. #nginx_stapling $1
  148. }
  149. # check an individual domain name
  150. function test_domain_name {
  151. if [ $1 ]; then
  152. TEST_DOMAIN_NAME=$1
  153. if [[ $TEST_DOMAIN_NAME != 'ttrss' ]]; then
  154. function_check validate_domain_name
  155. validate_domain_name
  156. if [[ $TEST_DOMAIN_NAME != $1 ]]; then
  157. echo $"Invalid domain name $TEST_DOMAIN_NAME"
  158. exit 8528
  159. fi
  160. fi
  161. fi
  162. }
  163. # Checks whether certificates were generated for the given hostname
  164. function check_certificates {
  165. if [ ! $1 ]; then
  166. return
  167. fi
  168. USE_LETSENCRYPT='no'
  169. if [ $2 ]; then
  170. USE_LETSENCRYPT=$2
  171. fi
  172. if [[ $USE_LETSENCRYPT == 'no' ]]; then
  173. if [ ! -f /etc/ssl/private/${1}.key ]; then
  174. echo $"Private certificate for ${CHECK_HOSTNAME} was not created"
  175. exit 63959
  176. fi
  177. if [ ! -f /etc/ssl/certs/${1}.crt ]; then
  178. echo $"Public certificate for ${CHECK_HOSTNAME} was not created"
  179. exit 7679
  180. fi
  181. if grep -q "${1}.pem" /etc/nginx/sites-available/${1}; then
  182. sed -i "s|${1}.pem|${1}.crt|g" /etc/nginx/sites-available/${1}
  183. fi
  184. else
  185. if [ ! -f /etc/letsencrypt/live/${1}/privkey.pem ]; then
  186. echo $"Private certificate for ${CHECK_HOSTNAME} was not created"
  187. exit 6282
  188. fi
  189. if [ ! -f /etc/letsencrypt/live/${1}/fullchain.pem ]; then
  190. echo $"Public certificate for ${CHECK_HOSTNAME} was not created"
  191. exit 5328
  192. fi
  193. if grep -q "${1}.crt" /etc/nginx/sites-available/${1}; then
  194. sed -i "s|${1}.crt|${1}.pem|g" /etc/nginx/sites-available/${1}
  195. fi
  196. fi
  197. if [ ! -f /etc/ssl/certs/${1}.dhparam ]; then
  198. echo $"Diffie–Hellman parameters for ${CHECK_HOSTNAME} were not created"
  199. exit 5989
  200. fi
  201. }
  202. function cert_exists {
  203. cert_type='dhparam'
  204. if [ $2 ]; then
  205. cert_type="$2"
  206. fi
  207. if [ -f /etc/ssl/certs/${1}.${cert_type} ]; then
  208. echo "1"
  209. else
  210. if [ -f /etc/letsencrypt/live/${1}/fullchain.${cert_type} ]; then
  211. echo "1"
  212. else
  213. echo "0"
  214. fi
  215. fi
  216. }
  217. function create_self_signed_cert {
  218. ${PROJECT_NAME}-addcert -h ${SITE_DOMAIN_NAME} --dhkey ${DH_KEYLENGTH}
  219. function_check check_certificates
  220. check_certificates ${SITE_DOMAIN_NAME}
  221. }
  222. function create_letsencrypt_cert {
  223. ${PROJECT_NAME}-addcert -e ${SITE_DOMAIN_NAME} -s ${LETSENCRYPT_SERVER} --dhkey ${DH_KEYLENGTH} --email ${MY_EMAIL_ADDRESS}
  224. if [ ! "$?" = "0" ]; then
  225. if [[ ${NO_SELF_SIGNED} == 'no' ]]; then
  226. echo $"Lets Encrypt failed for ${SITE_DOMAIN_NAME}, so try making a self-signed cert"
  227. ${PROJECT_NAME}-addcert -h ${SITE_DOMAIN_NAME} --dhkey ${DH_KEYLENGTH}
  228. function_check check_certificates
  229. check_certificates ${SITE_DOMAIN_NAME}
  230. else
  231. echo $"Lets Encrypt failed for $SITE_DOMAIN_NAME"
  232. exit 682529
  233. fi
  234. return
  235. fi
  236. function_check check_certificates
  237. check_certificates ${SITE_DOMAIN_NAME} 'yes'
  238. }
  239. function create_site_certificate {
  240. SITE_DOMAIN_NAME="$1"
  241. # if yes then only "valid" certs are allowed, not self-signed
  242. NO_SELF_SIGNED='no'
  243. if [ $2 ]; then
  244. NO_SELF_SIGNED="$2"
  245. fi
  246. if [[ $ONION_ONLY == "no" ]]; then
  247. if [[ "$(cert_exists ${SITE_DOMAIN_NAME})" == "0" ]]; then
  248. if [[ $LETSENCRYPT_ENABLED != "yes" ]]; then
  249. create_self_signed_cert
  250. else
  251. create_letsencrypt_cert
  252. fi
  253. else
  254. if [[ $LETSENCRYPT_ENABLED == "yes" ]]; then
  255. if [[ "$(cert_exists ${SITE_DOMAIN_NAME} pem)" == "0" ]]; then
  256. create_letsencrypt_cert
  257. fi
  258. fi
  259. fi
  260. fi
  261. }
  262. # script to automatically renew any Let's Encrypt certificates
  263. function letsencrypt_renewals {
  264. if [[ $ONION_ONLY != "no" ]]; then
  265. return
  266. fi
  267. renewals_script=/tmp/renewals_letsencrypt
  268. renewals_retry_script=/tmp/renewals_retry_letsencrypt
  269. renewal_failure_msg=$'The certificate for $LETSENCRYPT_DOMAIN could not be renewed'
  270. renewal_email_title=$'${PROJECT_NAME} Lets Encrypt certificate renewal'
  271. # the main script tries to renew once per month
  272. echo '#!/bin/bash' > $renewals_script
  273. echo '' >> $renewals_script
  274. echo "PROJECT_NAME='${PROJECT_NAME}'" >> $renewals_script
  275. echo 'COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt' >> $renewals_script
  276. echo '' >> $renewals_script
  277. echo 'if [ -d /etc/letsencrypt ]; then' >> $renewals_script
  278. echo ' if [ -f ~/letsencrypt_failed ]; then' >> $renewals_script
  279. echo ' rm ~/letsencrypt_failed' >> $renewals_script
  280. echo ' fi' >> $renewals_script
  281. echo -n ' ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | ' >> $renewals_script
  282. echo -n "awk -F ':' '{print " >> $renewals_script
  283. echo -n '$2' >> $renewals_script
  284. echo "}')" >> $renewals_script
  285. echo ' ADMIN_EMAIL_ADDRESS=$ADMIN_USERNAME@$HOSTNAME' >> $renewals_script
  286. echo ' for d in /etc/letsencrypt/live/*/ ; do' >> $renewals_script
  287. echo -n ' LETSENCRYPT_DOMAIN=$(echo "$d" | ' >> $renewals_script
  288. echo -n "awk -F '/' '{print " >> $renewals_script
  289. echo -n '$5' >> $renewals_script
  290. echo "}')" >> $renewals_script
  291. echo ' if [ -f /etc/nginx/sites-available/$LETSENCRYPT_DOMAIN ]; then' >> $renewals_script
  292. echo ' ${PROJECT_NAME}-renew-cert -h $LETSENCRYPT_DOMAIN -p letsencrypt' >> $renewals_script
  293. echo ' if [ ! "$?" = "0" ]; then' >> $renewals_script
  294. echo " echo \"${renewal_failure_msg}\" > ~/temp_renewletsencrypt.txt" >> $renewals_script
  295. echo ' echo "" >> ~/temp_renewletsencrypt.txt' >> $renewals_script
  296. echo ' ${PROJECT_NAME}-renew-cert -h $LETSENCRYPT_DOMAIN -p letsencrypt 2>> ~/temp_renewletsencrypt.txt' >> $renewals_script
  297. echo -n " cat ~/temp_renewletsencrypt.txt | mail -s \"${renewal_email_title}\" " >> $renewals_script
  298. echo '$ADMIN_EMAIL_ADDRESS' >> $renewals_script
  299. echo ' rm ~/temp_renewletsencrypt.txt' >> $renewals_script
  300. echo ' if [ ! -f ~/letsencrypt_failed ]; then' >> $renewals_script
  301. echo ' touch ~/letsencrypt_failed' >> $renewals_script
  302. echo ' fi' >> $renewals_script
  303. echo ' fi' >> $renewals_script
  304. echo ' fi' >> $renewals_script
  305. echo ' done' >> $renewals_script
  306. echo 'fi' >> $renewals_script
  307. chmod +x $renewals_script
  308. if [ ! -f /etc/cron.monthly/letsencrypt ]; then
  309. cp $renewals_script /etc/cron.monthly/letsencrypt
  310. else
  311. HASH1=$(sha256sum $renewals_script | awk -F ' ' '{print $1}')
  312. HASH2=$(sha256sum /etc/cron.monthly/letsencrypt | awk -F ' ' '{print $1}')
  313. if [[ "$HASH1" != "$HASH2" ]]; then
  314. cp $renewals_script /etc/cron.monthly/letsencrypt
  315. fi
  316. fi
  317. rm $renewals_script
  318. # a secondary script keeps trying to renew after a failure
  319. echo '#!/bin/bash' > $renewals_retry_script
  320. echo '' >> $renewals_retry_script
  321. echo "PROJECT_NAME='${PROJECT_NAME}'" >> $renewals_retry_script
  322. echo 'COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt' >> $renewals_retry_script
  323. echo '' >> $renewals_retry_script
  324. echo 'if [ -d /etc/letsencrypt ]; then' >> $renewals_retry_script
  325. echo ' if [ -f ~/letsencrypt_failed ]; then' >> $renewals_retry_script
  326. echo ' rm ~/letsencrypt_failed' >> $renewals_retry_script
  327. echo -n ' ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | ' >> $renewals_retry_script
  328. echo -n "awk -F ':' '{print " >> $renewals_retry_script
  329. echo -n '$2' >> $renewals_retry_script
  330. echo "}')" >> $renewals_retry_script
  331. echo ' ADMIN_EMAIL_ADDRESS=$ADMIN_USERNAME@$HOSTNAME' >> $renewals_retry_script
  332. echo ' for d in /etc/letsencrypt/live/*/ ; do' >> $renewals_retry_script
  333. echo -n ' LETSENCRYPT_DOMAIN=$(echo "$d" | ' >> $renewals_retry_script
  334. echo -n "awk -F '/' '{print " >> $renewals_retry_script
  335. echo -n '$5' >> $renewals_retry_script
  336. echo "}')" >> $renewals_retry_script
  337. echo ' if [ -f /etc/nginx/sites-available/$LETSENCRYPT_DOMAIN ]; then' >> $renewals_retry_script
  338. echo ' ${PROJECT_NAME}-renew-cert -h $LETSENCRYPT_DOMAIN -p letsencrypt' >> $renewals_retry_script
  339. echo ' if [ ! "$?" = "0" ]; then' >> $renewals_retry_script
  340. echo " echo \"${renewal_failure_msg}\" > ~/temp_renewletsencrypt.txt" >> $renewals_retry_script
  341. echo ' echo "" >> ~/temp_renewletsencrypt.txt' >> $renewals_retry_script
  342. echo ' ${PROJECT_NAME}-renew-cert -h $LETSENCRYPT_DOMAIN -p letsencrypt 2>> ~/temp_renewletsencrypt.txt' >> $renewals_retry_script
  343. echo -n " cat ~/temp_renewletsencrypt.txt | mail -s \"${renewal_email_title}\" " >> $renewals_retry_script
  344. echo '$ADMIN_EMAIL_ADDRESS' >> $renewals_retry_script
  345. echo ' rm ~/temp_renewletsencrypt.txt' >> $renewals_retry_script
  346. echo ' if [ ! -f ~/letsencrypt_failed ]; then' >> $renewals_retry_script
  347. echo ' touch ~/letsencrypt_failed' >> $renewals_retry_script
  348. echo ' fi' >> $renewals_retry_script
  349. echo ' fi' >> $renewals_retry_script
  350. echo ' fi' >> $renewals_retry_script
  351. echo ' done' >> $renewals_retry_script
  352. echo ' fi' >> $renewals_retry_script
  353. echo 'fi' >> $renewals_retry_script
  354. chmod +x $renewals_retry_script
  355. if [ ! -f /etc/cron.daily/letsencrypt ]; then
  356. cp $renewals_retry_script /etc/cron.daily/letsencrypt
  357. else
  358. HASH1=$(sha256sum $renewals_retry_script | awk -F ' ' '{print $1}')
  359. HASH2=$(sha256sum /etc/cron.daily/letsencrypt | awk -F ' ' '{print $1}')
  360. if [[ "$HASH1" != "$HASH2" ]]; then
  361. cp $renewals_retry_script /etc/cron.daily/letsencrypt
  362. fi
  363. fi
  364. rm $renewals_retry_script
  365. }
  366. function configure_php {
  367. sed -i "s/memory_limit = 128M/memory_limit = ${MAX_PHP_MEMORY}M/g" /etc/php/7.0/fpm/php.ini
  368. sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/7.0/fpm/php.ini
  369. sed -i "s/memory_limit = -1/memory_limit = ${MAX_PHP_MEMORY}M/g" /etc/php/7.0/cli/php.ini
  370. sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 50M/g" /etc/php/7.0/fpm/php.ini
  371. sed -i "s/post_max_size = 8M/post_max_size = 50M/g" /etc/php/7.0/fpm/php.ini
  372. }
  373. function install_web_server_access_control {
  374. if [ ! -f /etc/pam.d/nginx ]; then
  375. echo '#%PAM-1.0' > /etc/pam.d/nginx
  376. echo '@include common-auth' >> /etc/pam.d/nginx
  377. echo '@include common-account' >> /etc/pam.d/nginx
  378. echo '@include common-session' >> /etc/pam.d/nginx
  379. fi
  380. }
  381. function install_dynamicdns {
  382. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  383. return
  384. fi
  385. if [[ $ONION_ONLY != "no" ]]; then
  386. return
  387. fi
  388. CURR_INADYN_COMMIT=$(get_completion_param "inadyn commit")
  389. if [[ "${CURR_INADYN_COMMIT}" == "${INADYN_COMMIT}" ]]; then
  390. return
  391. fi
  392. # update to the next commit
  393. function_check set_repo_commit
  394. set_repo_commit $INSTALL_DIR/inadyn "inadyn commit" "$INADYN_COMMIT" $INADYN_REPO
  395. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  396. return
  397. fi
  398. # Here we compile from source because the current package
  399. # doesn't support https, which could result in passwords
  400. # being leaked
  401. # Debian version 1.99.4-1
  402. # https version 1.99.8
  403. apt-get -yq install build-essential curl libgnutls28-dev automake1.11
  404. if [ ! -d $INSTALL_DIR/inadyn ]; then
  405. if [ -d /repos/inadyn ]; then
  406. mkdir $INSTALL_DIR/inadyn
  407. cp -r -p /repos/inadyn/. $INSTALL_DIR/inadyn
  408. cd $INSTALL_DIR/inadyn
  409. git pull
  410. else
  411. git_clone $INADYN_REPO $INSTALL_DIR/inadyn
  412. fi
  413. fi
  414. if [ ! -d $INSTALL_DIR/inadyn ]; then
  415. echo 'inadyn repo not cloned'
  416. echo -n | openssl s_client -showcerts -connect github.com:443 -CApath /etc/ssl/certs
  417. exit 6785
  418. fi
  419. cd $INSTALL_DIR/inadyn
  420. git checkout $INADYN_COMMIT -b $INADYN_COMMIT
  421. set_completion_param "inadyn commit" "$INADYN_COMMIT"
  422. #./autogen.sh
  423. ./configure
  424. if [ ! "$?" = "0" ]; then
  425. exit 74890
  426. fi
  427. USE_OPENSSL=1 make
  428. if [ ! "$?" = "0" ]; then
  429. exit 74858
  430. fi
  431. make install
  432. if [ ! "$?" = "0" ]; then
  433. exit 3785
  434. fi
  435. # create an unprivileged user
  436. #chmod 600 /etc/shadow
  437. #chmod 600 /etc/gshadow
  438. #useradd -r -s /bin/false debian-inadyn
  439. #chmod 0000 /etc/shadow
  440. #chmod 0000 /etc/gshadow
  441. # create a configuration file
  442. echo 'background' > /etc/inadyn.conf
  443. echo 'verbose 1' >> /etc/inadyn.conf
  444. echo 'period 300' >> /etc/inadyn.conf
  445. echo 'startup-delay 60' >> /etc/inadyn.conf
  446. echo 'cache-dir /run/inadyn' >> /etc/inadyn.conf
  447. echo 'logfile /dev/null' >> /etc/inadyn.conf
  448. chmod 600 /etc/inadyn.conf
  449. echo '[Unit]' > /etc/systemd/system/inadyn.service
  450. echo 'Description=inadyn (DynDNS updater)' >> /etc/systemd/system/inadyn.service
  451. echo 'After=network.target' >> /etc/systemd/system/inadyn.service
  452. echo '' >> /etc/systemd/system/inadyn.service
  453. echo '[Service]' >> /etc/systemd/system/inadyn.service
  454. echo 'ExecStart=/usr/local/sbin/inadyn --config /etc/inadyn.conf' >> /etc/systemd/system/inadyn.service
  455. echo 'Restart=always' >> /etc/systemd/system/inadyn.service
  456. echo 'Type=forking' >> /etc/systemd/system/inadyn.service
  457. echo '' >> /etc/systemd/system/inadyn.service
  458. echo '[Install]' >> /etc/systemd/system/inadyn.service
  459. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/inadyn.service
  460. systemctl enable inadyn
  461. systemctl start inadyn
  462. systemctl daemon-reload
  463. mark_completed $FUNCNAME
  464. }
  465. function update_default_search_engine {
  466. for d in /home/*/ ; do
  467. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  468. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  469. if ! grep -q "WWW_HOME" /home/$USERNAME/.bashrc; then
  470. if ! grep -q 'controluser' /home/$USERNAME/.bashrc; then
  471. if ! grep -q 'export WWW_HOME=' /home/$USERNAME/.bashrc; then
  472. echo "export WWW_HOME=$DEFAULT_SEARCH" >> /home/$USERNAME/.bashrc
  473. else
  474. sed -i "s|export WWW_HOME=.*|export WWW_HOME=$DEFAULT_SEARCH|g" /home/$USERNAME/.bashrc
  475. fi
  476. else
  477. if ! grep -q 'export WWW_HOME=' /home/$USERNAME/.bashrc; then
  478. sed -i "/controluser/i export WWW_HOME=$DEFAULT_SEARCH" /home/$USERNAME/.bashrc
  479. else
  480. sed -i "s|export WWW_HOME=.*|export WWW_HOME=$DEFAULT_SEARCH|g" /home/$USERNAME/.bashrc
  481. fi
  482. fi
  483. fi
  484. fi
  485. done
  486. }
  487. function install_command_line_browser {
  488. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  489. return
  490. fi
  491. apt-get -yq install elinks
  492. update_default_search_engine
  493. mark_completed $FUNCNAME
  494. }
  495. function mesh_web_server {
  496. if [ -d /etc/apache2 ]; then
  497. chroot "$rootdir" apt-get -yq remove --purge apache2
  498. chroot "$rootdir" rm -rf /etc/apache2
  499. fi
  500. chroot "$rootdir" apt-get -yq install nginx
  501. if [ ! -d $rootdir/etc/nginx ]; then
  502. echo $'Unable to install web server'
  503. exit 346825
  504. fi
  505. }
  506. function install_web_server {
  507. if [ $INSTALLING_MESH ]; then
  508. mesh_web_server
  509. return
  510. fi
  511. # update to the next commit
  512. function_check set_repo_commit
  513. set_repo_commit $INSTALL_DIR/nginx_ensite "nginx-ensite commit" "$NGINX_ENSITE_COMMIT" $NGINX_ENSITE_REPO
  514. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  515. return
  516. fi
  517. # remove apache
  518. apt-get -yq remove --purge apache2
  519. if [ -d /etc/apache2 ]; then
  520. rm -rf /etc/apache2
  521. fi
  522. # install nginx
  523. apt-get -yq install nginx
  524. apt-get -yq install php-fpm git
  525. # Turn off logs by default
  526. sed -i 's|access_log.*|access_log = /dev/null;|g' /etc/nginx/nginx.conf
  527. sed -i 's|error_log.*|error_log = /dev/null;|g' /etc/nginx/nginx.conf
  528. # limit the number of php processes
  529. sed -i 's/; process.max =.*/process.max = 32/g' /etc/php/7.0/fpm/php-fpm.conf
  530. #sed -i 's/;process_control_timeout =.*/process_control_timeout = 300/g' /etc/php/7.0/fpm/php-fpm.conf
  531. sed -i 's|;systemd_interval.*|systemd_interval = 10|g' /etc/php/7.0/fpm/php-fpm.conf
  532. sed -i 's|;emergency_restart_threshold.*|emergency_restart_threshold = 2|g' /etc/php/7.0/fpm/php-fpm.conf
  533. sed -i 's|emergency_restart_threshold.*|emergency_restart_threshold = 2|g' /etc/php/7.0/fpm/php-fpm.conf
  534. sed -i 's|;emergency_restart_interval.*|emergency_restart_interval = 1m|g' /etc/php/7.0/fpm/php-fpm.conf
  535. sed -i 's|emergency_restart_interval.*|emergency_restart_interval = 1m|g' /etc/php/7.0/fpm/php-fpm.conf
  536. sed -i 's|;process_control_timeout.*|process_control_timeout = 10s|g' /etc/php/7.0/fpm/php-fpm.conf
  537. sed -i 's|process_control_timeout.*|process_control_timeout = 10s|g' /etc/php/7.0/fpm/php-fpm.conf
  538. if ! grep -q "pm.max_children" /etc/php/7.0/fpm/php-fpm.conf; then
  539. echo 'pm = static' >> /etc/php/7.0/fpm/php-fpm.conf
  540. echo 'pm.max_children = 10' >> /etc/php/7.0/fpm/php-fpm.conf
  541. echo 'pm.start_servers = 2' >> /etc/php/7.0/fpm/php-fpm.conf
  542. echo 'pm.min_spare_servers = 2' >> /etc/php/7.0/fpm/php-fpm.conf
  543. echo 'pm.max_spare_servers = 5' >> /etc/php/7.0/fpm/php-fpm.conf
  544. echo 'pm.max_requests = 10' >> /etc/php/7.0/fpm/php-fpm.conf
  545. fi
  546. if ! grep -q "request_terminate_timeout" /etc/php/7.0/fpm/php-fpm.conf; then
  547. echo 'request_terminate_timeout = 30' >> /etc/php/7.0/fpm/php-fpm.conf
  548. else
  549. sed -i 's|request_terminate_timeout =.*|request_terminate_timeout = 30|g' >> /etc/php/7.0/fpm/php-fpm.conf
  550. fi
  551. sed -i 's|max_execution_time =.*|max_execution_time = 30|g' /etc/php/7.0/fpm/php.ini
  552. if [ ! -d /etc/nginx ]; then
  553. echo $"ERROR: nginx does not appear to have installed. $CHECK_MESSAGE"
  554. exit 51
  555. fi
  556. # Nginx settings
  557. echo 'user www-data;' > /etc/nginx/nginx.conf
  558. #echo "worker_processes; $CPU_CORES" >> /etc/nginx/nginx.conf
  559. echo 'pid /run/nginx.pid;' >> /etc/nginx/nginx.conf
  560. echo '' >> /etc/nginx/nginx.conf
  561. echo 'events {' >> /etc/nginx/nginx.conf
  562. echo ' worker_connections 50;' >> /etc/nginx/nginx.conf
  563. echo ' # multi_accept on;' >> /etc/nginx/nginx.conf
  564. echo '}' >> /etc/nginx/nginx.conf
  565. echo '' >> /etc/nginx/nginx.conf
  566. echo 'http {' >> /etc/nginx/nginx.conf
  567. echo ' # limit the number of connections per single IP' >> /etc/nginx/nginx.conf
  568. echo ' limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;' >> /etc/nginx/nginx.conf
  569. echo '' >> /etc/nginx/nginx.conf
  570. echo ' # limit the number of requests for a given session' >> /etc/nginx/nginx.conf
  571. echo ' limit_req_zone $binary_remote_addr zone=req_limit_per_ip:10m rate=140r/s;' >> /etc/nginx/nginx.conf
  572. echo '' >> /etc/nginx/nginx.conf
  573. echo ' # if the request body size is more than the buffer size, then the entire (or partial) request body is written into a temporary file' >> /etc/nginx/nginx.conf
  574. echo ' client_body_buffer_size 128k;' >> /etc/nginx/nginx.conf
  575. echo '' >> /etc/nginx/nginx.conf
  576. echo ' # headerbuffer size for the request header from client, its set for testing purpose' >> /etc/nginx/nginx.conf
  577. echo ' client_header_buffer_size 3m;' >> /etc/nginx/nginx.conf
  578. echo '' >> /etc/nginx/nginx.conf
  579. echo ' # maximum number and size of buffers for large headers to read from client request' >> /etc/nginx/nginx.conf
  580. echo ' large_client_header_buffers 4 256k;' >> /etc/nginx/nginx.conf
  581. echo '' >> /etc/nginx/nginx.conf
  582. echo ' # read timeout for the request body from client, its set for testing purpose' >> /etc/nginx/nginx.conf
  583. echo ' client_body_timeout 3m;' >> /etc/nginx/nginx.conf
  584. echo '' >> /etc/nginx/nginx.conf
  585. echo ' # how long to wait for the client to send a request header, its set for testing purpose' >> /etc/nginx/nginx.conf
  586. echo ' client_header_timeout 3m;' >> /etc/nginx/nginx.conf
  587. echo '' >> /etc/nginx/nginx.conf
  588. echo ' ##' >> /etc/nginx/nginx.conf
  589. echo ' # Basic Settings' >> /etc/nginx/nginx.conf
  590. echo ' ##' >> /etc/nginx/nginx.conf
  591. echo '' >> /etc/nginx/nginx.conf
  592. echo ' sendfile on;' >> /etc/nginx/nginx.conf
  593. echo ' tcp_nopush on;' >> /etc/nginx/nginx.conf
  594. echo ' tcp_nodelay on;' >> /etc/nginx/nginx.conf
  595. echo ' keepalive_timeout 65;' >> /etc/nginx/nginx.conf
  596. echo ' types_hash_max_size 2048;' >> /etc/nginx/nginx.conf
  597. echo ' server_tokens off;' >> /etc/nginx/nginx.conf
  598. echo '' >> /etc/nginx/nginx.conf
  599. echo ' # server_names_hash_bucket_size 64;' >> /etc/nginx/nginx.conf
  600. echo ' # server_name_in_redirect off;' >> /etc/nginx/nginx.conf
  601. echo '' >> /etc/nginx/nginx.conf
  602. echo ' include /etc/nginx/mime.types;' >> /etc/nginx/nginx.conf
  603. echo ' default_type application/octet-stream;' >> /etc/nginx/nginx.conf
  604. echo '' >> /etc/nginx/nginx.conf
  605. echo ' ##' >> /etc/nginx/nginx.conf
  606. echo ' # Logging Settings' >> /etc/nginx/nginx.conf
  607. echo ' ##' >> /etc/nginx/nginx.conf
  608. echo '' >> /etc/nginx/nginx.conf
  609. echo ' access_log /dev/null;' >> /etc/nginx/nginx.conf
  610. echo ' error_log /dev/null;' >> /etc/nginx/nginx.conf
  611. echo '' >> /etc/nginx/nginx.conf
  612. echo ' ###' >> /etc/nginx/nginx.conf
  613. echo ' # Gzip Settings' >> /etc/nginx/nginx.conf
  614. echo ' ##' >> /etc/nginx/nginx.conf
  615. echo ' gzip on;' >> /etc/nginx/nginx.conf
  616. echo ' gzip_disable "msie6";' >> /etc/nginx/nginx.conf
  617. echo '' >> /etc/nginx/nginx.conf
  618. echo ' # gzip_vary on;' >> /etc/nginx/nginx.conf
  619. echo ' # gzip_proxied any;' >> /etc/nginx/nginx.conf
  620. echo ' # gzip_comp_level 6;' >> /etc/nginx/nginx.conf
  621. echo ' # gzip_buffers 16 8k;' >> /etc/nginx/nginx.conf
  622. echo ' # gzip_http_version 1.1;' >> /etc/nginx/nginx.conf
  623. echo ' # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;' >> /etc/nginx/nginx.conf
  624. echo '' >> /etc/nginx/nginx.conf
  625. echo ' ##' >> /etc/nginx/nginx.conf
  626. echo ' # Virtual Host Configs' >> /etc/nginx/nginx.conf
  627. echo ' ##' >> /etc/nginx/nginx.conf
  628. echo '' >> /etc/nginx/nginx.conf
  629. echo ' include /etc/nginx/conf.d/*.conf;' >> /etc/nginx/nginx.conf
  630. echo ' include /etc/nginx/sites-enabled/*;' >> /etc/nginx/nginx.conf
  631. echo '}' >> /etc/nginx/nginx.conf
  632. # install a script to easily enable and disable nginx virtual hosts
  633. if [ ! -d $INSTALL_DIR ]; then
  634. mkdir $INSTALL_DIR
  635. fi
  636. cd $INSTALL_DIR
  637. function_check git_clone
  638. git_clone $NGINX_ENSITE_REPO $INSTALL_DIR/nginx_ensite
  639. cd $INSTALL_DIR/nginx_ensite
  640. git checkout $NGINX_ENSITE_COMMIT -b $NGINX_ENSITE_COMMIT
  641. set_completion_param "nginx-ensite commit" "$NGINX_ENSITE_COMMIT"
  642. make install
  643. nginx_dissite default
  644. function_check configure_firewall_for_web_access
  645. configure_firewall_for_web_access
  646. mark_completed $FUNCNAME
  647. }
  648. function remove_certs {
  649. domain_name=$1
  650. if [ ! $domain_name ]; then
  651. return
  652. fi
  653. if [ -f /etc/ssl/certs/${domain_name}.dhparam ]; then
  654. rm /etc/ssl/certs/${domain_name}.dhparam
  655. fi
  656. if [ -f /etc/ssl/certs/${domain_name}.pem ]; then
  657. rm /etc/ssl/certs/${domain_name}.pem
  658. fi
  659. if [ -f /etc/ssl/certs/${domain_name}.crt ]; then
  660. rm /etc/ssl/certs/${domain_name}.crt
  661. fi
  662. if [ -f /etc/ssl/private/${domain_name}.key ]; then
  663. rm /etc/ssl/private/${domain_name}.key
  664. fi
  665. }
  666. function configure_firewall_for_web_access {
  667. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  668. return
  669. fi
  670. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  671. # docker does its own firewalling
  672. return
  673. fi
  674. if [[ $ONION_ONLY != "no" ]]; then
  675. return
  676. fi
  677. firewall_add HTTP 80 tcp
  678. firewall_add HTTPS 443 tcp
  679. mark_completed $FUNCNAME
  680. }
  681. function update_default_domain {
  682. echo $'Updating default domain'
  683. if [[ $ONION_ONLY == 'no' ]]; then
  684. if [ -d /etc/prosody ]; then
  685. if [ -f /etc/mumble-server.ini ]; then
  686. if [ ! -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
  687. if ! grep -q "mumble.pem" /etc/mumble-server.ini; then
  688. sed -i 's|sslCert=.*|sslCert=/var/lib/mumble-server/mumble.pem|g' /etc/mumble-server.ini
  689. sed -i 's|sslKey=.*|sslKey=/var/lib/mumble-server/mumble.key|g' /etc/mumble-server.ini
  690. systemctl restart mumble
  691. fi
  692. else
  693. if ! grep -q "${DEFAULT_DOMAIN_NAME}.pem" /etc/mumble-server.ini; then
  694. usermod -a -G ssl-cert mumble-server
  695. sed -i "s|sslCert=.*|sslCert=/etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/mumble-server.ini
  696. sed -i "s|sslKey=.*|sslKey=/etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key|g" /etc/mumble-server.ini
  697. systemctl restart mumble
  698. fi
  699. fi
  700. fi
  701. if [ ! -d /etc/prosody/certs ]; then
  702. mkdir /etc/prosody/certs
  703. fi
  704. cp /etc/ssl/private/xmpp* /etc/prosody/certs
  705. cp /etc/ssl/certs/xmpp* /etc/prosody/certs
  706. if [ /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
  707. usermod -a -G ssl-cert prosody
  708. if grep -q "/etc/prosody/certs/xmpp.key" /etc/prosody/conf.avail/xmpp.cfg.lua; then
  709. sed -i "s|/etc/prosody/certs/xmpp.key|/etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key|g" /etc/prosody/conf.avail/xmpp.cfg.lua
  710. fi
  711. if grep -q "/etc/prosody/certs/xmpp.crt" /etc/prosody/conf.avail/xmpp.cfg.lua; then
  712. sed -i "s|/etc/prosody/certs/xmpp.crt|/etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/prosody/conf.avail/xmpp.cfg.lua
  713. fi
  714. if grep -q "/etc/prosody/certs/xmpp.key" /etc/prosody/prosody.cfg.lua; then
  715. sed -i "s|/etc/prosody/certs/xmpp.key|/etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key|g" /etc/prosody/prosody.cfg.lua
  716. fi
  717. if grep -q "/etc/prosody/certs/xmpp.crt" /etc/prosody/prosody.cfg.lua; then
  718. sed -i "s|/etc/prosody/certs/xmpp.crt|/etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/prosody/prosody.cfg.lua
  719. fi
  720. fi
  721. if grep -q "/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.key" /etc/prosody/conf.avail/xmpp.cfg.lua; then
  722. sed -i "s|/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.key|/etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key|g" /etc/prosody/conf.avail/xmpp.cfg.lua
  723. fi
  724. if grep -q "/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.pem" /etc/prosody/conf.avail/xmpp.cfg.lua; then
  725. sed -i "s|/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.pem|/etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/prosody/conf.avail/xmpp.cfg.lua
  726. fi
  727. if grep -q "/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.key" /etc/prosody/prosody.cfg.lua; then
  728. sed -i "s|/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.key|/etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key|g" /etc/prosody/prosody.cfg.lua
  729. fi
  730. if grep -q "/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.pem" /etc/prosody/prosody.cfg.lua; then
  731. sed -i "s|/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.pem|/etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/prosody/prosody.cfg.lua
  732. fi
  733. chown -R prosody:default /etc/prosody
  734. chmod -R 700 /etc/prosody/certs/*
  735. chmod 600 /etc/prosody/prosody.cfg.lua
  736. cp -r $INSTALL_DIR/prosody-modules/* /var/lib/prosody/prosody-modules/
  737. chown -R prosody:prosody /var/lib/prosody/prosody-modules
  738. systemctl reload prosody
  739. fi
  740. if [ -d /home/znc/.znc ]; then
  741. echo $'znc found'
  742. if [[ "$(cert_exists ${DEFAULT_DOMAIN_NAME} pem)" == "1" ]]; then
  743. pkill znc
  744. cat /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem /etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key > /home/znc/.znc/znc.pem
  745. chown znc:znc /home/znc/.znc/znc.pem
  746. chmod 700 /home/znc/.znc/znc.pem
  747. sed -i "s|CertFile =.*|CertFile = /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem" /etc/ngircd/ngircd.conf
  748. sed -i "s|DHFile =.*|DHFile = /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.dhparam" /etc/ngircd/ngircd.conf
  749. sed -i "s|KeyFile =.*|KeyFile = /etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key" /etc/ngircd/ngircd.conf
  750. echo $'irc certificates updated'
  751. systemctl restart ngircd
  752. su -c 'znc' - znc
  753. fi
  754. fi
  755. if [ ${#DEFAULT_DOMAIN_NAME} -gt 0 ]; then
  756. if [ -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
  757. if [ -d /etc/dovecot ]; then
  758. if ! grep -q "ssl_cert = </etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem" /etc/dovecot/conf.d/10-ssl.conf; then
  759. sed -i "s|#ssl_cert =.*|ssl_cert = </etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/dovecot/conf.d/10-ssl.conf
  760. sed -i "s|ssl_cert =.*|ssl_cert = </etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/dovecot/conf.d/10-ssl.conf
  761. systemctl restart dovecot
  762. fi
  763. fi
  764. if [ -d /etc/exim4 ]; then
  765. cp /etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/{fullchain,privkey}.pem /etc/exim4/
  766. chown root:Debian-exim /etc/exim4/*.pem
  767. chmod 640 /etc/exim4/*.pem
  768. sed -i "s|MAIN_TLS_CERTIFICATE =.*|MAIN_TLS_CERTIFICATE = /etc/exim4/fullchain.pem|g" /etc/exim4/conf.d/main/03_exim4-config_tlsoptions
  769. sed -i "s|MAIN_TLS_CERTIFICATE =.*|MAIN_TLS_CERTIFICATE = /etc/exim4/fullchain.pem|g" /etc/exim4/exim4.conf.template
  770. sed -i "s|MAIN_TLS_PRIVATEKEY =.*|MAIN_TLS_PRIVATEKEY = /etc/exim4/privkey.pem|g" /etc/exim4/conf.d/main/03_exim4-config_tlsoptions
  771. sed -i "s|MAIN_TLS_PRIVATEKEY =.*|MAIN_TLS_PRIVATEKEY = /etc/exim4/privkey.pem|g" /etc/exim4/exim4.conf.template
  772. systemctl restart exim4
  773. fi
  774. fi
  775. fi
  776. fi
  777. }
  778. function create_default_web_site {
  779. if [ ! -f /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME} ]; then
  780. # create a web site for the default domain
  781. if [ ! -d /var/www/${DEFAULT_DOMAIN_NAME}/htdocs ]; then
  782. mkdir -p /var/www/${DEFAULT_DOMAIN_NAME}/htdocs
  783. if [ -d /root/${PROJECT_NAME} ]; then
  784. cd /root/${PROJECT_NAME}/website
  785. ./deploy.sh EN /var/www/${DEFAULT_DOMAIN_NAME}/htdocs
  786. else
  787. if [ -d /home/${MY_USERNAME}/${PROJECT_NAME} ]; then
  788. cd /home/${MY_USERNAME}/${PROJECT_NAME}
  789. ./deploy.sh EN /var/www/${DEFAULT_DOMAIN_NAME}/htdocs
  790. fi
  791. fi
  792. fi
  793. # add a config for the default domain
  794. nginx_site=/etc/nginx/sites-available/$DEFAULT_DOMAIN_NAME
  795. if [[ $ONION_ONLY == "no" ]]; then
  796. function_check nginx_http_redirect
  797. nginx_http_redirect $DEFAULT_DOMAIN_NAME
  798. echo 'server {' >> $nginx_site
  799. echo ' listen 443 ssl;' >> $nginx_site
  800. echo ' listen [::]:443 ssl;' >> $nginx_site
  801. echo " server_name $DEFAULT_DOMAIN_NAME;" >> $nginx_site
  802. echo '' >> $nginx_site
  803. echo ' # Security' >> $nginx_site
  804. function_check nginx_ssl
  805. nginx_ssl $DEFAULT_DOMAIN_NAME mobile
  806. function_check nginx_disable_sniffing
  807. nginx_disable_sniffing $DEFAULT_DOMAIN_NAME
  808. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $nginx_site
  809. echo '' >> $nginx_site
  810. echo ' # Logs' >> $nginx_site
  811. echo ' access_log /dev/null;' >> $nginx_site
  812. echo ' error_log /dev/null;' >> $nginx_site
  813. echo '' >> $nginx_site
  814. echo ' # Root' >> $nginx_site
  815. echo " root /var/www/$DEFAULT_DOMAIN_NAME/htdocs;" >> $nginx_site
  816. echo '' >> $nginx_site
  817. echo ' # Index' >> $nginx_site
  818. echo ' index index.html;' >> $nginx_site
  819. echo '' >> $nginx_site
  820. echo ' # Location' >> $nginx_site
  821. echo ' location / {' >> $nginx_site
  822. function_check nginx_limits
  823. nginx_limits $DEFAULT_DOMAIN_NAME '15m'
  824. echo ' }' >> $nginx_site
  825. echo '' >> $nginx_site
  826. echo ' # Restrict access that is unnecessary anyway' >> $nginx_site
  827. echo ' location ~ /\.(ht|git) {' >> $nginx_site
  828. echo ' deny all;' >> $nginx_site
  829. echo ' }' >> $nginx_site
  830. echo '}' >> $nginx_site
  831. else
  832. echo -n '' > $nginx_site
  833. fi
  834. echo 'server {' >> $nginx_site
  835. echo " listen 127.0.0.1:$DEFAULT_DOMAIN_ONION_PORT default_server;" >> $nginx_site
  836. echo " server_name $DEFAULT_DOMAIN_NAME;" >> $nginx_site
  837. echo '' >> $nginx_site
  838. function_check nginx_disable_sniffing
  839. nginx_disable_sniffing $DEFAULT_DOMAIN_NAME
  840. echo '' >> $nginx_site
  841. echo ' # Logs' >> $nginx_site
  842. echo ' access_log /dev/null;' >> $nginx_site
  843. echo ' error_log /dev/null;' >> $nginx_site
  844. echo '' >> $nginx_site
  845. echo ' # Root' >> $nginx_site
  846. echo " root /var/www/$DEFAULT_DOMAIN_NAME/htdocs;" >> $nginx_site
  847. echo '' >> $nginx_site
  848. echo ' # Location' >> $nginx_site
  849. echo ' location / {' >> $nginx_site
  850. function_check nginx_limits
  851. nginx_limits $DEFAULT_DOMAIN_NAME '15m'
  852. echo ' }' >> $nginx_site
  853. echo '' >> $nginx_site
  854. echo ' # Restrict access that is unnecessary anyway' >> $nginx_site
  855. echo ' location ~ /\.(ht|git) {' >> $nginx_site
  856. echo ' deny all;' >> $nginx_site
  857. echo ' }' >> $nginx_site
  858. echo '}' >> $nginx_site
  859. if [ ! -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
  860. function_check create_site_certificate
  861. create_site_certificate $DEFAULT_DOMAIN_NAME 'yes'
  862. fi
  863. nginx_ensite $DEFAULT_DOMAIN_NAME
  864. fi
  865. }
  866. function install_composer {
  867. # curl -sS https://getcomposer.org/installer | php
  868. if [ -f ~/${PROJECT_NAME}/image_build/composer_install ]; then
  869. cat ~/${PROJECT_NAME}/image_build/composer_install | php
  870. else
  871. if [ -f /home/$MY_USERNAME/${PROJECT_NAME}/image_build/composer_install ]; then
  872. cat /home/$MY_USERNAME/${PROJECT_NAME}/image_build/composer_install | php
  873. fi
  874. fi
  875. if [ -f composer.phar ]; then
  876. cp composer.phar composer
  877. fi
  878. php composer.phar install
  879. if [ ! "$?" = "0" ]; then
  880. echo $'Unable to run composer install'
  881. exit 7252198
  882. fi
  883. }
  884. function email_disable_chunking {
  885. if [ -f /etc/exim4/conf.d/main/04_exim4-config_chunking ]; then
  886. return
  887. fi
  888. echo "chunking_advertise_hosts =" > /etc/exim4/conf.d/main/04_exim4-config_chunking
  889. update-exim4.conf
  890. dpkg-reconfigure --frontend noninteractive exim4-config
  891. systemctl restart exim4
  892. }
  893. function email_install_tls {
  894. tls_config_file=/etc/exim4/conf.d/main/03_exim4-config_tlsoptions
  895. tls_auth_config_file=/etc/exim4/conf.d/auth/30_exim4-config_examples
  896. email_config_changed=
  897. if [ ! -f $tls_config_file ]; then
  898. tls_config_file=/etc/exim4/exim4.conf.template
  899. tls_auth_config_file=$tls_config_file
  900. fi
  901. if [ ! -f /etc/ssl/certs/exim.dhparam ]; then
  902. ${PROJECT_NAME}-addcert -h exim --dhkey $DH_KEYLENGTH
  903. check_certificates exim
  904. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  905. chown root:Debian-exim /etc/exim4/exim.dhparam
  906. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  907. email_config_changed=1
  908. fi
  909. if ! grep -q 'MAIN_TLS_ENABLE = true' $tls_config_file; then
  910. sed -i "/.ifdef MAIN_HARDCODE_PRIMARY_HOSTNAME/i\MAIN_HARDCODE_PRIMARY_HOSTNAME =\nMAIN_TLS_ENABLE = true" $tls_config_file
  911. email_config_changed=1
  912. fi
  913. if ! grep -q "tls_on_connect_ports=465" $tls_config_file; then
  914. sed -i '/SSL configuration for exim/i\tls_on_connect_ports=465' $tls_config_file
  915. email_config_changed=1
  916. fi
  917. if grep -q '# login_saslauthd_server' $tls_auth_config_file; then
  918. sed -i '/login_saslauthd_server/,/.endif/ s/# *//' $tls_auth_config_file
  919. email_config_changed=1
  920. fi
  921. if [ -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
  922. cp /etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/fullchain.pem /etc/exim4/
  923. chown root:Debian-exim /etc/exim4/*.pem
  924. chmod 640 /etc/exim4/*.pem
  925. if ! grep -q "MAIN_TLS_CERTIFICATE = /etc/exim4/fullchain.pem" $tls_config_file; then
  926. sed -i "/.ifdef MAIN_TLS_CERTKEY/i\MAIN_TLS_CERTIFICATE = /etc/exim4/fullchain.pem" $tls_config_file
  927. email_config_changed=1
  928. fi
  929. fi
  930. if [ -f /etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key ]; then
  931. cp /etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/privkey.pem /etc/exim4/
  932. chown root:Debian-exim /etc/exim4/*.pem
  933. chmod 640 /etc/exim4/*.pem
  934. if ! grep -q "MAIN_TLS_PRIVATEKEY = /etc/exim4/privkey.pem" $tls_config_file; then
  935. sed -i "/.ifndef MAIN_TLS_PRIVATEKEY/i\MAIN_TLS_PRIVATEKEY = /etc/exim4/privkey.pem" $tls_config_file
  936. email_config_changed=1
  937. fi
  938. fi
  939. if ! grep -q "SMTPLISTENEROPTIONS='-oX 465:25:587" /etc/default/exim4; then
  940. sed -i "s|SMTPLISTENEROPTIONS=.*|SMTPLISTENEROPTIONS='-oX 465:25:587 -oP /var/run/exim4/exim.pid'|g" /etc/default/exim4
  941. email_config_changed=1
  942. fi
  943. if [ $email_config_changed ]; then
  944. systemctl restart saslauthd
  945. systemctl restart exim4
  946. fi
  947. }
  948. function install_web_local_user_interface {
  949. # TODO
  950. # This is intended as a placeholder for a potential local web user interface
  951. # similar to Plinth or the yunohost admin interface
  952. local_hostname=$(cat /etc/avahi/avahi-daemon.conf | grep 'host-name' | awk -F '=' '{print $2}').local
  953. mkdir -p /var/www/${local_hostname}/htdocs
  954. echo '<html>' > /var/www/${local_hostname}/htdocs/index.html
  955. echo ' <body>' >> /var/www/${local_hostname}/htdocs/index.html
  956. echo " This is a test on $local_hostname" >> /var/www/${local_hostname}/htdocs/index.html
  957. echo ' </body>' >> /var/www/${local_hostname}/htdocs/index.html
  958. echo '</html>' >> /var/www/${local_hostname}/htdocs/index.html
  959. chown -R www-data:www-data /var/www/${local_hostname}/htdocs
  960. nginx_file=/etc/nginx/sites-available/$local_hostname
  961. echo 'server {' > $nginx_file
  962. echo ' listen 80;' >> $nginx_file
  963. echo ' listen [::]:80;' >> $nginx_file
  964. echo " server_name ${local_hostname};" >> $nginx_file
  965. echo " root /var/www/${local_hostname}/htdocs;" >> $nginx_file
  966. echo ' index index.html;' >> $nginx_file
  967. echo '}' >> $nginx_file
  968. nginx_ensite $local_hostname
  969. }
  970. # NOTE: deliberately no exit 0