freedombone-utils-web 46KB

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