freedombone-utils-web 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. function nginx_keybase {
  150. # creates files suitable for keybase.io verification
  151. domain_name=$1
  152. filename=/etc/nginx/sites-available/$domain_name
  153. echo '' >> $filename
  154. echo " # make sure webfinger and other well known services aren't blocked" >> $filename
  155. echo ' # by denying dot files and rewrite request to the front controller' >> $filename
  156. echo ' location ^~ /.well-known/ {' >> $filename
  157. echo ' allow all;' >> $filename
  158. echo ' }' >> $filename
  159. if [ ! -d /var/www/${domain_name}/htdocs/.well-known ]; then
  160. mkdir -p /var/www/${domain_name}/htdocs/.well-known
  161. fi
  162. if [ ! -f /var/www/${domain_name}/htdocs/keybase.txt ]; then
  163. touch /var/www/${domain_name}/htdocs/keybase.txt
  164. fi
  165. if [ ! -f /var/www/${domain_name}/htdocs/.well-known/keybase.txt ]; then
  166. touch /var/www/${domain_name}/htdocs/.well-known/keybase.txt
  167. fi
  168. }
  169. # check an individual domain name
  170. function test_domain_name {
  171. if [ $1 ]; then
  172. TEST_DOMAIN_NAME=$1
  173. if [[ $TEST_DOMAIN_NAME != 'ttrss' ]]; then
  174. function_check validate_domain_name
  175. validate_domain_name
  176. if [[ $TEST_DOMAIN_NAME != $1 ]]; then
  177. echo $"Invalid domain name $TEST_DOMAIN_NAME"
  178. exit 8528
  179. fi
  180. fi
  181. fi
  182. }
  183. # Checks whether certificates were generated for the given hostname
  184. function check_certificates {
  185. if [ ! $1 ]; then
  186. return
  187. fi
  188. USE_LETSENCRYPT='no'
  189. if [ $2 ]; then
  190. USE_LETSENCRYPT=$2
  191. fi
  192. if [[ $USE_LETSENCRYPT == 'no' ]]; then
  193. if [ ! -f /etc/ssl/private/${1}.key ]; then
  194. echo $"Private certificate for ${CHECK_HOSTNAME} was not created"
  195. exit 63959
  196. fi
  197. if [ ! -f /etc/ssl/certs/${1}.crt ]; then
  198. echo $"Public certificate for ${CHECK_HOSTNAME} was not created"
  199. exit 7679
  200. fi
  201. if grep -q "${1}.pem" /etc/nginx/sites-available/${1}; then
  202. sed -i "s|${1}.pem|${1}.crt|g" /etc/nginx/sites-available/${1}
  203. fi
  204. else
  205. if [ ! -f /etc/letsencrypt/live/${1}/privkey.pem ]; then
  206. echo $"Private certificate for ${CHECK_HOSTNAME} was not created"
  207. exit 6282
  208. fi
  209. if [ ! -f /etc/letsencrypt/live/${1}/fullchain.pem ]; then
  210. echo $"Public certificate for ${CHECK_HOSTNAME} was not created"
  211. exit 5328
  212. fi
  213. if grep -q "${1}.crt" /etc/nginx/sites-available/${1}; then
  214. sed -i "s|${1}.crt|${1}.pem|g" /etc/nginx/sites-available/${1}
  215. fi
  216. fi
  217. if [ ! -f /etc/ssl/certs/${1}.dhparam ]; then
  218. echo $"Diffie–Hellman parameters for ${CHECK_HOSTNAME} were not created"
  219. exit 5989
  220. fi
  221. }
  222. function cert_exists {
  223. cert_type='dhparam'
  224. if [ $2 ]; then
  225. cert_type="$2"
  226. fi
  227. if [ -f /etc/ssl/certs/${1}.${cert_type} ]; then
  228. echo "1"
  229. else
  230. if [ -f /etc/letsencrypt/live/${1}/fullchain.${cert_type} ]; then
  231. echo "1"
  232. else
  233. echo "0"
  234. fi
  235. fi
  236. }
  237. function create_self_signed_cert {
  238. ${PROJECT_NAME}-addcert -h ${SITE_DOMAIN_NAME} --dhkey ${DH_KEYLENGTH}
  239. function_check check_certificates
  240. check_certificates ${SITE_DOMAIN_NAME}
  241. }
  242. function create_letsencrypt_cert {
  243. ${PROJECT_NAME}-addcert -e ${SITE_DOMAIN_NAME} -s ${LETSENCRYPT_SERVER} --dhkey ${DH_KEYLENGTH} --email ${MY_EMAIL_ADDRESS}
  244. if [ ! "$?" = "0" ]; then
  245. if [[ ${NO_SELF_SIGNED} == 'no' ]]; then
  246. echo $"Lets Encrypt failed for ${SITE_DOMAIN_NAME}, so try making a self-signed cert"
  247. ${PROJECT_NAME}-addcert -h ${SITE_DOMAIN_NAME} --dhkey ${DH_KEYLENGTH}
  248. function_check check_certificates
  249. check_certificates ${SITE_DOMAIN_NAME}
  250. else
  251. echo $"Lets Encrypt failed for $SITE_DOMAIN_NAME"
  252. exit 682529
  253. fi
  254. return
  255. fi
  256. function_check check_certificates
  257. check_certificates ${SITE_DOMAIN_NAME} 'yes'
  258. }
  259. function create_site_certificate {
  260. SITE_DOMAIN_NAME="$1"
  261. # if yes then only "valid" certs are allowed, not self-signed
  262. NO_SELF_SIGNED='no'
  263. if [ $2 ]; then
  264. NO_SELF_SIGNED="$2"
  265. fi
  266. if [[ $ONION_ONLY == "no" ]]; then
  267. if [[ "$(cert_exists ${SITE_DOMAIN_NAME})" == "0" ]]; then
  268. if [[ $LETSENCRYPT_ENABLED != "yes" ]]; then
  269. create_self_signed_cert
  270. else
  271. create_letsencrypt_cert
  272. fi
  273. else
  274. if [[ $LETSENCRYPT_ENABLED == "yes" ]]; then
  275. if [[ "$(cert_exists ${SITE_DOMAIN_NAME} pem)" == "0" ]]; then
  276. create_letsencrypt_cert
  277. fi
  278. fi
  279. fi
  280. fi
  281. }
  282. # script to automatically renew any Let's Encrypt certificates
  283. function letsencrypt_renewals {
  284. if [[ $ONION_ONLY != "no" ]]; then
  285. return
  286. fi
  287. renewals_script=/tmp/renewals_letsencrypt
  288. renewals_retry_script=/tmp/renewals_retry_letsencrypt
  289. renewal_failure_msg=$'The certificate for $LETSENCRYPT_DOMAIN could not be renewed'
  290. renewal_email_title=$'${PROJECT_NAME} Lets Encrypt certificate renewal'
  291. # the main script tries to renew once per month
  292. echo '#!/bin/bash' > $renewals_script
  293. echo '' >> $renewals_script
  294. echo "PROJECT_NAME='${PROJECT_NAME}'" >> $renewals_script
  295. echo 'COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt' >> $renewals_script
  296. echo '' >> $renewals_script
  297. echo 'if [ -d /etc/letsencrypt ]; then' >> $renewals_script
  298. echo ' if [ -f ~/letsencrypt_failed ]; then' >> $renewals_script
  299. echo ' rm ~/letsencrypt_failed' >> $renewals_script
  300. echo ' fi' >> $renewals_script
  301. echo -n ' ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | ' >> $renewals_script
  302. echo -n "awk -F ':' '{print " >> $renewals_script
  303. echo -n '$2' >> $renewals_script
  304. echo "}')" >> $renewals_script
  305. echo ' ADMIN_EMAIL_ADDRESS=$ADMIN_USERNAME@$HOSTNAME' >> $renewals_script
  306. echo ' for d in /etc/letsencrypt/live/*/ ; do' >> $renewals_script
  307. echo -n ' LETSENCRYPT_DOMAIN=$(echo "$d" | ' >> $renewals_script
  308. echo -n "awk -F '/' '{print " >> $renewals_script
  309. echo -n '$5' >> $renewals_script
  310. echo "}')" >> $renewals_script
  311. echo ' if [ -f /etc/nginx/sites-available/$LETSENCRYPT_DOMAIN ]; then' >> $renewals_script
  312. echo ' ${PROJECT_NAME}-renew-cert -h $LETSENCRYPT_DOMAIN -p letsencrypt' >> $renewals_script
  313. echo ' if [ ! "$?" = "0" ]; then' >> $renewals_script
  314. echo " echo \"${renewal_failure_msg}\" > ~/temp_renewletsencrypt.txt" >> $renewals_script
  315. echo ' echo "" >> ~/temp_renewletsencrypt.txt' >> $renewals_script
  316. echo ' ${PROJECT_NAME}-renew-cert -h $LETSENCRYPT_DOMAIN -p letsencrypt 2>> ~/temp_renewletsencrypt.txt' >> $renewals_script
  317. echo -n " cat ~/temp_renewletsencrypt.txt | mail -s \"${renewal_email_title}\" " >> $renewals_script
  318. echo '$ADMIN_EMAIL_ADDRESS' >> $renewals_script
  319. echo ' rm ~/temp_renewletsencrypt.txt' >> $renewals_script
  320. echo ' if [ ! -f ~/letsencrypt_failed ]; then' >> $renewals_script
  321. echo ' touch ~/letsencrypt_failed' >> $renewals_script
  322. echo ' fi' >> $renewals_script
  323. echo ' fi' >> $renewals_script
  324. echo ' fi' >> $renewals_script
  325. echo ' done' >> $renewals_script
  326. echo 'fi' >> $renewals_script
  327. chmod +x $renewals_script
  328. if [ ! -f /etc/cron.monthly/letsencrypt ]; then
  329. cp $renewals_script /etc/cron.monthly/letsencrypt
  330. else
  331. HASH1=$(sha256sum $renewals_script | awk -F ' ' '{print $1}')
  332. HASH2=$(sha256sum /etc/cron.monthly/letsencrypt | awk -F ' ' '{print $1}')
  333. if [[ "$HASH1" != "$HASH2" ]]; then
  334. cp $renewals_script /etc/cron.monthly/letsencrypt
  335. fi
  336. fi
  337. rm $renewals_script
  338. # a secondary script keeps trying to renew after a failure
  339. echo '#!/bin/bash' > $renewals_retry_script
  340. echo '' >> $renewals_retry_script
  341. echo "PROJECT_NAME='${PROJECT_NAME}'" >> $renewals_retry_script
  342. echo 'COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt' >> $renewals_retry_script
  343. echo '' >> $renewals_retry_script
  344. echo 'if [ -d /etc/letsencrypt ]; then' >> $renewals_retry_script
  345. echo ' if [ -f ~/letsencrypt_failed ]; then' >> $renewals_retry_script
  346. echo ' rm ~/letsencrypt_failed' >> $renewals_retry_script
  347. echo -n ' ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | ' >> $renewals_retry_script
  348. echo -n "awk -F ':' '{print " >> $renewals_retry_script
  349. echo -n '$2' >> $renewals_retry_script
  350. echo "}')" >> $renewals_retry_script
  351. echo ' ADMIN_EMAIL_ADDRESS=$ADMIN_USERNAME@$HOSTNAME' >> $renewals_retry_script
  352. echo ' for d in /etc/letsencrypt/live/*/ ; do' >> $renewals_retry_script
  353. echo -n ' LETSENCRYPT_DOMAIN=$(echo "$d" | ' >> $renewals_retry_script
  354. echo -n "awk -F '/' '{print " >> $renewals_retry_script
  355. echo -n '$5' >> $renewals_retry_script
  356. echo "}')" >> $renewals_retry_script
  357. echo ' if [ -f /etc/nginx/sites-available/$LETSENCRYPT_DOMAIN ]; then' >> $renewals_retry_script
  358. echo ' ${PROJECT_NAME}-renew-cert -h $LETSENCRYPT_DOMAIN -p letsencrypt' >> $renewals_retry_script
  359. echo ' if [ ! "$?" = "0" ]; then' >> $renewals_retry_script
  360. echo " echo \"${renewal_failure_msg}\" > ~/temp_renewletsencrypt.txt" >> $renewals_retry_script
  361. echo ' echo "" >> ~/temp_renewletsencrypt.txt' >> $renewals_retry_script
  362. echo ' ${PROJECT_NAME}-renew-cert -h $LETSENCRYPT_DOMAIN -p letsencrypt 2>> ~/temp_renewletsencrypt.txt' >> $renewals_retry_script
  363. echo -n " cat ~/temp_renewletsencrypt.txt | mail -s \"${renewal_email_title}\" " >> $renewals_retry_script
  364. echo '$ADMIN_EMAIL_ADDRESS' >> $renewals_retry_script
  365. echo ' rm ~/temp_renewletsencrypt.txt' >> $renewals_retry_script
  366. echo ' if [ ! -f ~/letsencrypt_failed ]; then' >> $renewals_retry_script
  367. echo ' touch ~/letsencrypt_failed' >> $renewals_retry_script
  368. echo ' fi' >> $renewals_retry_script
  369. echo ' fi' >> $renewals_retry_script
  370. echo ' fi' >> $renewals_retry_script
  371. echo ' done' >> $renewals_retry_script
  372. echo ' fi' >> $renewals_retry_script
  373. echo 'fi' >> $renewals_retry_script
  374. chmod +x $renewals_retry_script
  375. if [ ! -f /etc/cron.daily/letsencrypt ]; then
  376. cp $renewals_retry_script /etc/cron.daily/letsencrypt
  377. else
  378. HASH1=$(sha256sum $renewals_retry_script | awk -F ' ' '{print $1}')
  379. HASH2=$(sha256sum /etc/cron.daily/letsencrypt | awk -F ' ' '{print $1}')
  380. if [[ "$HASH1" != "$HASH2" ]]; then
  381. cp $renewals_retry_script /etc/cron.daily/letsencrypt
  382. fi
  383. fi
  384. rm $renewals_retry_script
  385. }
  386. function configure_php {
  387. sed -i "s/memory_limit = 128M/memory_limit = ${MAX_PHP_MEMORY}M/g" /etc/php/7.0/fpm/php.ini
  388. sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/7.0/fpm/php.ini
  389. sed -i "s/memory_limit = -1/memory_limit = ${MAX_PHP_MEMORY}M/g" /etc/php/7.0/cli/php.ini
  390. sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 50M/g" /etc/php/7.0/fpm/php.ini
  391. sed -i "s/post_max_size = 8M/post_max_size = 50M/g" /etc/php/7.0/fpm/php.ini
  392. }
  393. function install_web_server_access_control {
  394. if [ ! -f /etc/pam.d/nginx ]; then
  395. echo '#%PAM-1.0' > /etc/pam.d/nginx
  396. echo '@include common-auth' >> /etc/pam.d/nginx
  397. echo '@include common-account' >> /etc/pam.d/nginx
  398. echo '@include common-session' >> /etc/pam.d/nginx
  399. fi
  400. }
  401. function install_dynamicdns {
  402. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  403. return
  404. fi
  405. if [[ $ONION_ONLY != "no" ]]; then
  406. return
  407. fi
  408. CURR_INADYN_COMMIT=$(get_completion_param "inadyn commit")
  409. if [[ "${CURR_INADYN_COMMIT}" == "${INADYN_COMMIT}" ]]; then
  410. return
  411. fi
  412. # update to the next commit
  413. function_check set_repo_commit
  414. set_repo_commit $INSTALL_DIR/inadyn "inadyn commit" "$INADYN_COMMIT" $INADYN_REPO
  415. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  416. return
  417. fi
  418. # Here we compile from source because the current package
  419. # doesn't support https, which could result in passwords
  420. # being leaked
  421. # Debian version 1.99.4-1
  422. # https version 1.99.8
  423. apt-get -yq install build-essential curl libgnutls28-dev automake1.11
  424. if [ ! -d $INSTALL_DIR/inadyn ]; then
  425. if [ -d /repos/inadyn ]; then
  426. mkdir $INSTALL_DIR/inadyn
  427. cp -r -p /repos/inadyn/. $INSTALL_DIR/inadyn
  428. cd $INSTALL_DIR/inadyn
  429. git pull
  430. else
  431. git_clone $INADYN_REPO $INSTALL_DIR/inadyn
  432. fi
  433. fi
  434. if [ ! -d $INSTALL_DIR/inadyn ]; then
  435. echo 'inadyn repo not cloned'
  436. echo -n | openssl s_client -showcerts -connect github.com:443 -CApath /etc/ssl/certs
  437. exit 6785
  438. fi
  439. cd $INSTALL_DIR/inadyn
  440. git checkout $INADYN_COMMIT -b $INADYN_COMMIT
  441. set_completion_param "inadyn commit" "$INADYN_COMMIT"
  442. #./autogen.sh
  443. ./configure
  444. if [ ! "$?" = "0" ]; then
  445. exit 74890
  446. fi
  447. USE_OPENSSL=1 make
  448. if [ ! "$?" = "0" ]; then
  449. exit 74858
  450. fi
  451. make install
  452. if [ ! "$?" = "0" ]; then
  453. exit 3785
  454. fi
  455. # create an unprivileged user
  456. #chmod 600 /etc/shadow
  457. #chmod 600 /etc/gshadow
  458. #useradd -r -s /bin/false debian-inadyn
  459. #chmod 0000 /etc/shadow
  460. #chmod 0000 /etc/gshadow
  461. # create a configuration file
  462. echo 'background' > /etc/inadyn.conf
  463. echo 'verbose 1' >> /etc/inadyn.conf
  464. echo 'period 300' >> /etc/inadyn.conf
  465. echo 'startup-delay 60' >> /etc/inadyn.conf
  466. echo 'cache-dir /run/inadyn' >> /etc/inadyn.conf
  467. echo 'logfile /dev/null' >> /etc/inadyn.conf
  468. chmod 600 /etc/inadyn.conf
  469. echo '[Unit]' > /etc/systemd/system/inadyn.service
  470. echo 'Description=inadyn (DynDNS updater)' >> /etc/systemd/system/inadyn.service
  471. echo 'After=network.target' >> /etc/systemd/system/inadyn.service
  472. echo '' >> /etc/systemd/system/inadyn.service
  473. echo '[Service]' >> /etc/systemd/system/inadyn.service
  474. echo 'ExecStart=/usr/local/sbin/inadyn --config /etc/inadyn.conf' >> /etc/systemd/system/inadyn.service
  475. echo 'Restart=always' >> /etc/systemd/system/inadyn.service
  476. echo 'Type=forking' >> /etc/systemd/system/inadyn.service
  477. echo '' >> /etc/systemd/system/inadyn.service
  478. echo '[Install]' >> /etc/systemd/system/inadyn.service
  479. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/inadyn.service
  480. systemctl enable inadyn
  481. systemctl start inadyn
  482. systemctl daemon-reload
  483. mark_completed $FUNCNAME
  484. }
  485. function install_command_line_browser {
  486. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  487. return
  488. fi
  489. apt-get -yq install elinks
  490. # set the home page
  491. if ! grep -q "WWW_HOME" /home/$MY_USERNAME/.bashrc; then
  492. if ! grep -q 'controluser' /home/$MY_USERNAME/.bashrc; then
  493. echo "export WWW_HOME=$DEFAULT_SEARCH" >> /home/$MY_USERNAME/.bashrc
  494. else
  495. sed -i "/controluser/i export WWW_HOME=$DEFAULT_SEARCH" /home/$MY_USERNAME/.bashrc
  496. fi
  497. fi
  498. mark_completed $FUNCNAME
  499. }
  500. function mesh_web_server {
  501. if [ -d /etc/apache2 ]; then
  502. chroot "$rootdir" apt-get -yq remove --purge apache2
  503. chroot "$rootdir" rm -rf /etc/apache2
  504. fi
  505. chroot "$rootdir" apt-get -yq install nginx
  506. if [ ! -d $rootdir/etc/nginx ]; then
  507. echo $'Unable to install web server'
  508. exit 346825
  509. fi
  510. }
  511. function install_web_server {
  512. if [ $INSTALLING_MESH ]; then
  513. mesh_web_server
  514. return
  515. fi
  516. # update to the next commit
  517. function_check set_repo_commit
  518. set_repo_commit $INSTALL_DIR/nginx_ensite "nginx-ensite commit" "$NGINX_ENSITE_COMMIT" $NGINX_ENSITE_REPO
  519. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  520. return
  521. fi
  522. # remove apache
  523. apt-get -yq remove --purge apache2
  524. if [ -d /etc/apache2 ]; then
  525. rm -rf /etc/apache2
  526. fi
  527. # install nginx
  528. apt-get -yq install nginx
  529. apt-get -yq install php-fpm git
  530. # Turn off logs by default
  531. sed -i 's|access_log.*|access_log = /dev/null;|g' /etc/nginx/nginx.conf
  532. sed -i 's|error_log.*|error_log = /dev/null;|g' /etc/nginx/nginx.conf
  533. # limit the number of php processes
  534. sed -i 's/; process.max =.*/process.max = 32/g' /etc/php/7.0/fpm/php-fpm.conf
  535. #sed -i 's/;process_control_timeout =.*/process_control_timeout = 300/g' /etc/php/7.0/fpm/php-fpm.conf
  536. sed -i 's|;systemd_interval.*|systemd_interval = 10|g' /etc/php/7.0/fpm/php-fpm.conf
  537. sed -i 's|;emergency_restart_threshold.*|emergency_restart_threshold = 2|g' /etc/php/7.0/fpm/php-fpm.conf
  538. sed -i 's|emergency_restart_threshold.*|emergency_restart_threshold = 2|g' /etc/php/7.0/fpm/php-fpm.conf
  539. sed -i 's|;emergency_restart_interval.*|emergency_restart_interval = 1m|g' /etc/php/7.0/fpm/php-fpm.conf
  540. sed -i 's|emergency_restart_interval.*|emergency_restart_interval = 1m|g' /etc/php/7.0/fpm/php-fpm.conf
  541. sed -i 's|;process_control_timeout.*|process_control_timeout = 10s|g' /etc/php/7.0/fpm/php-fpm.conf
  542. sed -i 's|process_control_timeout.*|process_control_timeout = 10s|g' /etc/php/7.0/fpm/php-fpm.conf
  543. if ! grep -q "pm.max_children" /etc/php/7.0/fpm/php-fpm.conf; then
  544. echo 'pm = static' >> /etc/php/7.0/fpm/php-fpm.conf
  545. echo 'pm.max_children = 10' >> /etc/php/7.0/fpm/php-fpm.conf
  546. echo 'pm.start_servers = 2' >> /etc/php/7.0/fpm/php-fpm.conf
  547. echo 'pm.min_spare_servers = 2' >> /etc/php/7.0/fpm/php-fpm.conf
  548. echo 'pm.max_spare_servers = 5' >> /etc/php/7.0/fpm/php-fpm.conf
  549. echo 'pm.max_requests = 10' >> /etc/php/7.0/fpm/php-fpm.conf
  550. fi
  551. if ! grep -q "request_terminate_timeout" /etc/php/7.0/fpm/php-fpm.conf; then
  552. echo 'request_terminate_timeout = 30' >> /etc/php/7.0/fpm/php-fpm.conf
  553. else
  554. sed -i 's|request_terminate_timeout =.*|request_terminate_timeout = 30|g' >> /etc/php/7.0/fpm/php-fpm.conf
  555. fi
  556. sed -i 's|max_execution_time =.*|max_execution_time = 30|g' /etc/php/7.0/fpm/php.ini
  557. if [ ! -d /etc/nginx ]; then
  558. echo $"ERROR: nginx does not appear to have installed. $CHECK_MESSAGE"
  559. exit 51
  560. fi
  561. # Nginx settings
  562. echo 'user www-data;' > /etc/nginx/nginx.conf
  563. #echo "worker_processes; $CPU_CORES" >> /etc/nginx/nginx.conf
  564. echo 'pid /run/nginx.pid;' >> /etc/nginx/nginx.conf
  565. echo '' >> /etc/nginx/nginx.conf
  566. echo 'events {' >> /etc/nginx/nginx.conf
  567. echo ' worker_connections 50;' >> /etc/nginx/nginx.conf
  568. echo ' # multi_accept on;' >> /etc/nginx/nginx.conf
  569. echo '}' >> /etc/nginx/nginx.conf
  570. echo '' >> /etc/nginx/nginx.conf
  571. echo 'http {' >> /etc/nginx/nginx.conf
  572. echo ' # limit the number of connections per single IP' >> /etc/nginx/nginx.conf
  573. echo ' limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;' >> /etc/nginx/nginx.conf
  574. echo '' >> /etc/nginx/nginx.conf
  575. echo ' # limit the number of requests for a given session' >> /etc/nginx/nginx.conf
  576. echo ' limit_req_zone $binary_remote_addr zone=req_limit_per_ip:10m rate=140r/s;' >> /etc/nginx/nginx.conf
  577. echo '' >> /etc/nginx/nginx.conf
  578. 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
  579. echo ' client_body_buffer_size 128k;' >> /etc/nginx/nginx.conf
  580. echo '' >> /etc/nginx/nginx.conf
  581. echo ' # headerbuffer size for the request header from client, its set for testing purpose' >> /etc/nginx/nginx.conf
  582. echo ' client_header_buffer_size 3m;' >> /etc/nginx/nginx.conf
  583. echo '' >> /etc/nginx/nginx.conf
  584. echo ' # maximum number and size of buffers for large headers to read from client request' >> /etc/nginx/nginx.conf
  585. echo ' large_client_header_buffers 4 256k;' >> /etc/nginx/nginx.conf
  586. echo '' >> /etc/nginx/nginx.conf
  587. echo ' # read timeout for the request body from client, its set for testing purpose' >> /etc/nginx/nginx.conf
  588. echo ' client_body_timeout 3m;' >> /etc/nginx/nginx.conf
  589. echo '' >> /etc/nginx/nginx.conf
  590. echo ' # how long to wait for the client to send a request header, its set for testing purpose' >> /etc/nginx/nginx.conf
  591. echo ' client_header_timeout 3m;' >> /etc/nginx/nginx.conf
  592. echo '' >> /etc/nginx/nginx.conf
  593. echo ' ##' >> /etc/nginx/nginx.conf
  594. echo ' # Basic Settings' >> /etc/nginx/nginx.conf
  595. echo ' ##' >> /etc/nginx/nginx.conf
  596. echo '' >> /etc/nginx/nginx.conf
  597. echo ' sendfile on;' >> /etc/nginx/nginx.conf
  598. echo ' tcp_nopush on;' >> /etc/nginx/nginx.conf
  599. echo ' tcp_nodelay on;' >> /etc/nginx/nginx.conf
  600. echo ' keepalive_timeout 65;' >> /etc/nginx/nginx.conf
  601. echo ' types_hash_max_size 2048;' >> /etc/nginx/nginx.conf
  602. echo ' server_tokens off;' >> /etc/nginx/nginx.conf
  603. echo '' >> /etc/nginx/nginx.conf
  604. echo ' # server_names_hash_bucket_size 64;' >> /etc/nginx/nginx.conf
  605. echo ' # server_name_in_redirect off;' >> /etc/nginx/nginx.conf
  606. echo '' >> /etc/nginx/nginx.conf
  607. echo ' include /etc/nginx/mime.types;' >> /etc/nginx/nginx.conf
  608. echo ' default_type application/octet-stream;' >> /etc/nginx/nginx.conf
  609. echo '' >> /etc/nginx/nginx.conf
  610. echo ' ##' >> /etc/nginx/nginx.conf
  611. echo ' # Logging Settings' >> /etc/nginx/nginx.conf
  612. echo ' ##' >> /etc/nginx/nginx.conf
  613. echo '' >> /etc/nginx/nginx.conf
  614. echo ' access_log /dev/null;' >> /etc/nginx/nginx.conf
  615. echo ' error_log /dev/null;' >> /etc/nginx/nginx.conf
  616. echo '' >> /etc/nginx/nginx.conf
  617. echo ' ###' >> /etc/nginx/nginx.conf
  618. echo ' # Gzip Settings' >> /etc/nginx/nginx.conf
  619. echo ' ##' >> /etc/nginx/nginx.conf
  620. echo ' gzip on;' >> /etc/nginx/nginx.conf
  621. echo ' gzip_disable "msie6";' >> /etc/nginx/nginx.conf
  622. echo '' >> /etc/nginx/nginx.conf
  623. echo ' # gzip_vary on;' >> /etc/nginx/nginx.conf
  624. echo ' # gzip_proxied any;' >> /etc/nginx/nginx.conf
  625. echo ' # gzip_comp_level 6;' >> /etc/nginx/nginx.conf
  626. echo ' # gzip_buffers 16 8k;' >> /etc/nginx/nginx.conf
  627. echo ' # gzip_http_version 1.1;' >> /etc/nginx/nginx.conf
  628. echo ' # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;' >> /etc/nginx/nginx.conf
  629. echo '' >> /etc/nginx/nginx.conf
  630. echo ' ##' >> /etc/nginx/nginx.conf
  631. echo ' # Virtual Host Configs' >> /etc/nginx/nginx.conf
  632. echo ' ##' >> /etc/nginx/nginx.conf
  633. echo '' >> /etc/nginx/nginx.conf
  634. echo ' include /etc/nginx/conf.d/*.conf;' >> /etc/nginx/nginx.conf
  635. echo ' include /etc/nginx/sites-enabled/*;' >> /etc/nginx/nginx.conf
  636. echo '}' >> /etc/nginx/nginx.conf
  637. # install a script to easily enable and disable nginx virtual hosts
  638. if [ ! -d $INSTALL_DIR ]; then
  639. mkdir $INSTALL_DIR
  640. fi
  641. cd $INSTALL_DIR
  642. function_check git_clone
  643. git_clone $NGINX_ENSITE_REPO $INSTALL_DIR/nginx_ensite
  644. cd $INSTALL_DIR/nginx_ensite
  645. git checkout $NGINX_ENSITE_COMMIT -b $NGINX_ENSITE_COMMIT
  646. set_completion_param "nginx-ensite commit" "$NGINX_ENSITE_COMMIT"
  647. make install
  648. nginx_dissite default
  649. function_check configure_firewall_for_web_access
  650. configure_firewall_for_web_access
  651. mark_completed $FUNCNAME
  652. }
  653. function remove_certs {
  654. domain_name=$1
  655. if [ ! $domain_name ]; then
  656. return
  657. fi
  658. if [ -f /etc/ssl/certs/${domain_name}.dhparam ]; then
  659. rm /etc/ssl/certs/${domain_name}.dhparam
  660. fi
  661. if [ -f /etc/ssl/certs/${domain_name}.pem ]; then
  662. rm /etc/ssl/certs/${domain_name}.pem
  663. fi
  664. if [ -f /etc/ssl/certs/${domain_name}.crt ]; then
  665. rm /etc/ssl/certs/${domain_name}.crt
  666. fi
  667. if [ -f /etc/ssl/private/${domain_name}.key ]; then
  668. rm /etc/ssl/private/${domain_name}.key
  669. fi
  670. }
  671. function configure_firewall_for_web_access {
  672. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  673. return
  674. fi
  675. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  676. # docker does its own firewalling
  677. return
  678. fi
  679. if [[ $ONION_ONLY != "no" ]]; then
  680. return
  681. fi
  682. firewall_add HTTP 80 tcp
  683. firewall_add HTTPS 443 tcp
  684. mark_completed $FUNCNAME
  685. }
  686. function update_default_domain {
  687. echo $'Updating default domain'
  688. if [[ $ONION_ONLY == 'no' ]]; then
  689. if [ -d /etc/prosody ]; then
  690. if [ -f /etc/mumble-server.ini ]; then
  691. if [ ! -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
  692. if ! grep -q "mumble.pem" /etc/mumble-server.ini; then
  693. sed -i 's|sslCert=.*|sslCert=/var/lib/mumble-server/mumble.pem|g' /etc/mumble-server.ini
  694. sed -i 's|sslKey=.*|sslKey=/var/lib/mumble-server/mumble.key|g' /etc/mumble-server.ini
  695. systemctl restart mumble
  696. fi
  697. else
  698. if ! grep -q "${DEFAULT_DOMAIN_NAME}.pem" /etc/mumble-server.ini; then
  699. usermod -a -G ssl-cert mumble-server
  700. sed -i "s|sslCert=.*|sslCert=/etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/mumble-server.ini
  701. sed -i "s|sslKey=.*|sslKey=/etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key|g" /etc/mumble-server.ini
  702. systemctl restart mumble
  703. fi
  704. fi
  705. fi
  706. if [ ! -d /etc/prosody/certs ]; then
  707. mkdir /etc/prosody/certs
  708. fi
  709. cp /etc/ssl/private/xmpp* /etc/prosody/certs
  710. cp /etc/ssl/certs/xmpp* /etc/prosody/certs
  711. if [ /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
  712. usermod -a -G ssl-cert prosody
  713. sed -i "s|/etc/prosody/certs/xmpp.key|/etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key|g" /etc/prosody/conf.avail/xmpp.cfg.lua
  714. sed -i "s|/etc/prosody/certs/xmpp.crt|/etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/prosody/conf.avail/xmpp.cfg.lua
  715. sed -i "s|/etc/prosody/certs/xmpp.key|/etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key|g" /etc/prosody/prosody.cfg.lua
  716. sed -i "s|/etc/prosody/certs/xmpp.crt|/etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/prosody/prosody.cfg.lua
  717. fi
  718. if grep -q "/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.key" /etc/prosody/conf.avail/xmpp.cfg.lua; then
  719. 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
  720. fi
  721. if grep -q "/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.pem" /etc/prosody/conf.avail/xmpp.cfg.lua; then
  722. 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
  723. fi
  724. if grep -q "/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.key" /etc/prosody/prosody.cfg.lua; then
  725. sed -i "s|/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.key|/etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key|g" /etc/prosody/prosody.cfg.lua
  726. fi
  727. if grep -q "/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.pem" /etc/prosody/prosody.cfg.lua; then
  728. sed -i "s|/etc/prosody/certs/${DEFAULT_DOMAIN_NAME}.pem|/etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/prosody/prosody.cfg.lua
  729. fi
  730. chown -R prosody:default /etc/prosody
  731. chmod -R 700 /etc/prosody/certs/*
  732. chmod 600 /etc/prosody/prosody.cfg.lua
  733. cp -r $INSTALL_DIR/prosody-modules/* /var/lib/prosody/prosody-modules/
  734. chown -R prosody:prosody /var/lib/prosody/prosody-modules
  735. systemctl reload prosody
  736. fi
  737. if [ -d /home/znc/.znc ]; then
  738. echo $'znc found'
  739. if [[ "$(cert_exists ${DEFAULT_DOMAIN_NAME} pem)" == "1" ]]; then
  740. pkill znc
  741. cat /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem /etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key > /home/znc/.znc/znc.pem
  742. chown znc:znc /home/znc/.znc/znc.pem
  743. chmod 700 /home/znc/.znc/znc.pem
  744. sed -i "s|CertFile =.*|CertFile = /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem" /etc/ngircd/ngircd.conf
  745. sed -i "s|DHFile =.*|DHFile = /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.dhparam" /etc/ngircd/ngircd.conf
  746. sed -i "s|KeyFile =.*|KeyFile = /etc/ssl/private/${DEFAULT_DOMAIN_NAME}.key" /etc/ngircd/ngircd.conf
  747. echo $'irc certificates updated'
  748. systemctl restart ngircd
  749. su -c 'znc' - znc
  750. fi
  751. fi
  752. if [ -d /etc/dovecot ]; then
  753. if [ ${#DEFAULT_DOMAIN_NAME} -gt 0 ]; then
  754. if ! grep -q "ssl_cert = </etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem" /etc/dovecot/conf.d/10-ssl.conf; then
  755. sed -i "s|#ssl_cert =.*|ssl_cert = </etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/dovecot/conf.d/10-ssl.conf
  756. sed -i "s|ssl_cert =.*|ssl_cert = </etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem|g" /etc/dovecot/conf.d/10-ssl.conf
  757. systemctl restart dovecot
  758. fi
  759. fi
  760. fi
  761. fi
  762. }
  763. function create_default_web_site {
  764. if [ ! -f /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME} ]; then
  765. # create a web site for the default domain
  766. if [ ! -d /var/www/${DEFAULT_DOMAIN_NAME}/htdocs ]; then
  767. mkdir -p /var/www/${DEFAULT_DOMAIN_NAME}/htdocs
  768. if [ -d /root/${PROJECT_NAME} ]; then
  769. cd /root/${PROJECT_NAME}/website
  770. ./deploy.sh EN /var/www/${DEFAULT_DOMAIN_NAME}/htdocs
  771. else
  772. if [ -d /home/${MY_USERNAME}/${PROJECT_NAME} ]; then
  773. cd /home/${MY_USERNAME}/${PROJECT_NAME}
  774. ./deploy.sh EN /var/www/${DEFAULT_DOMAIN_NAME}/htdocs
  775. fi
  776. fi
  777. fi
  778. # add a config for the default domain
  779. nginx_site=/etc/nginx/sites-available/$DEFAULT_DOMAIN_NAME
  780. if [[ $ONION_ONLY == "no" ]]; then
  781. function_check nginx_http_redirect
  782. nginx_http_redirect $DEFAULT_DOMAIN_NAME
  783. echo 'server {' >> $nginx_site
  784. echo ' listen 443 ssl;' >> $nginx_site
  785. echo ' listen [::]:443 ssl;' >> $nginx_site
  786. echo " server_name $DEFAULT_DOMAIN_NAME;" >> $nginx_site
  787. echo '' >> $nginx_site
  788. echo ' # Security' >> $nginx_site
  789. function_check nginx_ssl
  790. nginx_ssl $DEFAULT_DOMAIN_NAME mobile
  791. function_check nginx_disable_sniffing
  792. nginx_disable_sniffing $DEFAULT_DOMAIN_NAME
  793. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $nginx_site
  794. echo '' >> $nginx_site
  795. echo ' # Logs' >> $nginx_site
  796. echo ' access_log /dev/null;' >> $nginx_site
  797. echo ' error_log /dev/null;' >> $nginx_site
  798. echo '' >> $nginx_site
  799. echo ' # Root' >> $nginx_site
  800. echo " root /var/www/$DEFAULT_DOMAIN_NAME/htdocs;" >> $nginx_site
  801. echo '' >> $nginx_site
  802. echo ' # Index' >> $nginx_site
  803. echo ' index index.html;' >> $nginx_site
  804. echo '' >> $nginx_site
  805. echo ' # Location' >> $nginx_site
  806. echo ' location / {' >> $nginx_site
  807. function_check nginx_limits
  808. nginx_limits $DEFAULT_DOMAIN_NAME '15m'
  809. echo ' }' >> $nginx_site
  810. echo '' >> $nginx_site
  811. echo ' # Restrict access that is unnecessary anyway' >> $nginx_site
  812. echo ' location ~ /\.(ht|git) {' >> $nginx_site
  813. echo ' deny all;' >> $nginx_site
  814. echo ' }' >> $nginx_site
  815. echo '}' >> $nginx_site
  816. else
  817. echo -n '' > $nginx_site
  818. fi
  819. echo 'server {' >> $nginx_site
  820. echo " listen 127.0.0.1:$DEFAULT_DOMAIN_ONION_PORT default_server;" >> $nginx_site
  821. echo " server_name $DEFAULT_DOMAIN_NAME;" >> $nginx_site
  822. echo '' >> $nginx_site
  823. function_check nginx_disable_sniffing
  824. nginx_disable_sniffing $DEFAULT_DOMAIN_NAME
  825. echo '' >> $nginx_site
  826. echo ' # Logs' >> $nginx_site
  827. echo ' access_log /dev/null;' >> $nginx_site
  828. echo ' error_log /dev/null;' >> $nginx_site
  829. echo '' >> $nginx_site
  830. echo ' # Root' >> $nginx_site
  831. echo " root /var/www/$DEFAULT_DOMAIN_NAME/htdocs;" >> $nginx_site
  832. echo '' >> $nginx_site
  833. echo ' # Location' >> $nginx_site
  834. echo ' location / {' >> $nginx_site
  835. function_check nginx_limits
  836. nginx_limits $DEFAULT_DOMAIN_NAME '15m'
  837. echo ' }' >> $nginx_site
  838. echo '' >> $nginx_site
  839. echo ' # Restrict access that is unnecessary anyway' >> $nginx_site
  840. echo ' location ~ /\.(ht|git) {' >> $nginx_site
  841. echo ' deny all;' >> $nginx_site
  842. echo ' }' >> $nginx_site
  843. echo '}' >> $nginx_site
  844. if [ ! -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
  845. function_check create_site_certificate
  846. create_site_certificate $DEFAULT_DOMAIN_NAME 'yes'
  847. fi
  848. nginx_ensite $DEFAULT_DOMAIN_NAME
  849. fi
  850. }
  851. function install_composer {
  852. # curl -sS https://getcomposer.org/installer | php
  853. if [ -f ~/${PROJECT_NAME}/image_build/composer_install ]; then
  854. cat ~/${PROJECT_NAME}/image_build/composer_install | php
  855. else
  856. if [ -f /home/$MY_USERNAME/${PROJECT_NAME}/image_build/composer_install ]; then
  857. cat /home/$MY_USERNAME/${PROJECT_NAME}/image_build/composer_install | php
  858. fi
  859. fi
  860. php composer.phar install
  861. if [ ! "$?" = "0" ]; then
  862. echo $'Unable to run composer install'
  863. exit 7252198
  864. fi
  865. }
  866. # NOTE: deliberately no exit 0