freedombone-utils-web 37KB

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