freedombone-utils-web 48KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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. if [ ! -d "/var/www/${local_hostname}/htdocs" ]; then
  1031. mkdir -p "/var/www/${local_hostname}/htdocs"
  1032. fi
  1033. { echo '<html>';
  1034. echo ' <body>';
  1035. echo " This is a test on ${local_hostname}";
  1036. echo ' </body>';
  1037. echo '</html>'; } > "/var/www/${local_hostname}/htdocs/index.html"
  1038. nginx_file=/etc/nginx/sites-available/$local_hostname
  1039. { echo 'server {';
  1040. echo ' listen 80;';
  1041. echo ' listen [::]:80;';
  1042. echo " server_name ${local_hostname};";
  1043. echo " root /var/www/${local_hostname}/htdocs;";
  1044. echo ' index index.html;';
  1045. echo '';
  1046. echo ' access_log /dev/null;';
  1047. echo ' error_log /dev/null;';
  1048. echo '';
  1049. echo ' location /icons {';
  1050. echo ' autoindex on;';
  1051. echo ' break;';
  1052. echo ' }';
  1053. echo '';
  1054. echo ' rewrite ^/plinth/(.*)$ /api.json last;';
  1055. echo '';
  1056. echo ' location / {';
  1057. echo " root /var/www/${local_hostname}/htdocs/plinth;";
  1058. echo ' index api.json /api.json;';
  1059. echo " error_page 405 = \$uri;";
  1060. echo ' }';
  1061. echo '}';
  1062. echo '';
  1063. echo 'server {';
  1064. echo ' listen 443 ssl;';
  1065. echo ' #listen [::]:443 ssl;';
  1066. echo " server_name ${local_hostname};";
  1067. echo " root /var/www/${local_hostname}/htdocs;";
  1068. echo ' index index.html;';
  1069. echo '';
  1070. echo ' access_log /dev/null;';
  1071. echo ' error_log /dev/null;';
  1072. echo ''; } > "$nginx_file"
  1073. nginx_ssl "${local_hostname}"
  1074. nginx_security_options "${local_hostname}"
  1075. { echo ' add_header Strict-Transport-Security max-age=0;';
  1076. echo '';
  1077. echo ' location /icons {';
  1078. echo ' autoindex on;';
  1079. echo ' break;';
  1080. echo ' }';
  1081. echo '';
  1082. echo ' rewrite ^/plinth/(.*)$ /api.json last;';
  1083. echo '';
  1084. echo ' location / {';
  1085. echo " root /var/www/${local_hostname}/htdocs/plinth;";
  1086. echo ' index api.json /api.json;';
  1087. echo " error_page 405 = \$uri;";
  1088. echo ' }';
  1089. echo '}'; } >> "$nginx_file"
  1090. if [ ! -f "/etc/ssl/certs/${local_hostname}.crt" ]; then
  1091. "${PROJECT_NAME}-addcert" -h "${local_hostname}" --dhkey "${DH_KEYLENGTH}"
  1092. fi
  1093. sed -i "s|ssl_certificate .*|ssl_certificate /etc/ssl/certs/${local_hostname}.crt;|g" "$nginx_file"
  1094. sed -i "s|ssl_certificate_key .*|ssl_certificate_key /etc/ssl/private/${local_hostname}.key;|g" "$nginx_file"
  1095. nginx_ensite "${local_hostname}"
  1096. # Compatibility with FreedomBox android app
  1097. # The installed apps get published to a json file called api.json
  1098. # in this directory
  1099. if [ ! -d "/var/www/${local_hostname}/htdocs/plinth" ]; then
  1100. mkdir -p "/var/www/${local_hostname}/htdocs/plinth"
  1101. fi
  1102. chown -R www-data:www-data "/var/www/${local_hostname}/htdocs"
  1103. }
  1104. # NOTE: deliberately no exit 0