freedombone-logging 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Turn logging on or off
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. export TEXTDOMAIN=${PROJECT_NAME}-logging
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. WEBSERVER_LOG_LEVEL='warn'
  33. # Shredding could be used here, but especially on microSD
  34. # or SSD it's debatable how useful shredding really is.
  35. # Also the shred command can be very slow on Beaglebone Black
  36. REMOVE_FILES_COMMAND='rm -rf'
  37. function turn_off_rsys_logging {
  38. sed -i 's|mail,news.none.*|mail,news.none /dev/null|g' /etc/rsyslog.conf
  39. sed -i 's|auth,authpriv.\*.*|auth,authpriv.\* /dev/null|g' /etc/rsyslog.conf
  40. sed -i 's|mail.info.*|mail.info /dev/null|g' /etc/rsyslog.conf
  41. sed -i 's|mail.warn.*|mail.warn /dev/null|g' /etc/rsyslog.conf
  42. sed -i 's|mail.err.*|mail.err /dev/null|g' /etc/rsyslog.conf
  43. sed -i 's|daemon.\*.*|daemon.\* /dev/null|g' /etc/rsyslog.conf
  44. sed -i 's|mail.\*.*|mail.\* /dev/null|g' /etc/rsyslog.conf
  45. sed -i 's|user.\*.*|user.\* /dev/null|g' /etc/rsyslog.conf
  46. sed -i 's|news.none;mail.none.*|news.none;mail.none /dev/null|g' /etc/rsyslog.conf
  47. sed -i 's|\*.\*;auth,authpriv.none.*|\*.\*;auth,authpriv.none /dev/null|g' /etc/rsyslog.conf
  48. sed -i 's|#cron.\*|cron.\*|g' /etc/rsyslog.conf
  49. sed -i 's|cron.\*.*|cron.\* /dev/null|g' /etc/rsyslog.conf
  50. $REMOVE_FILES_COMMAND /var/log/wtmp*
  51. $REMOVE_FILES_COMMAND /var/log/debug*
  52. $REMOVE_FILES_COMMAND /var/log/cron.*
  53. $REMOVE_FILES_COMMAND /var/log/auth.*
  54. $REMOVE_FILES_COMMAND /var/log/mail.*
  55. $REMOVE_FILES_COMMAND /var/log/daemon.*
  56. $REMOVE_FILES_COMMAND /var/log/user.*
  57. $REMOVE_FILES_COMMAND /var/log/messages*
  58. }
  59. function turn_on_rsys_logging {
  60. sed -i 's|mail,news.none.*|mail,news.none -/var/log/messages|g' /etc/rsyslog.conf
  61. sed -i 's|auth,authpriv.\*.*|auth,authpriv.\* /var/log/auth.log|g' /etc/rsyslog.conf
  62. sed -i 's|mail.info.*|mail.info -/var/log/mail.info|g' /etc/rsyslog.conf
  63. sed -i 's|mail.warn.*|mail.warn -/var/log/mail.warn|g' /etc/rsyslog.conf
  64. sed -i 's|mail.err.*|mail.err /var/log/mail.err|g' /etc/rsyslog.conf
  65. sed -i 's|daemon.\*.*|daemon.\* -/var/log/daemon.log|g' /etc/rsyslog.conf
  66. sed -i 's|mail.\*.*|mail.\* -/var/log/mail.log|g' /etc/rsyslog.conf
  67. sed -i 's|user.\*.*|user.\* -/var/log/user.log|g' /etc/rsyslog.conf
  68. sed -i 's|news.none;mail.none.*|news.none;mail.none -/var/log/debug|g' /etc/rsyslog.conf
  69. sed -i 's|\*.\*;auth,authpriv.none.*|\*.\*;auth,authpriv.none -/var/log/syslog|g' /etc/rsyslog.conf
  70. sed -i 's|#cron.\*|cron.\*|g' /etc/rsyslog.conf
  71. sed -i 's|cron.\*.*|cron.\* /var/log/cron.log|g' /etc/rsyslog.conf
  72. }
  73. if [ ! "$1" ]; then
  74. exit 1
  75. fi
  76. if [[ "$1" == "on" || "$1" == "On" || "$1" == "ON" ]]; then
  77. if [ -f /var/lib/matrix/homeserver.yaml ]; then
  78. sed -i 's|log_file:.*|log_file: /etc/matrix/homeserver.log|g' /var/lib/matrix/homeserver.yaml
  79. if ! grep -q "#log_config:" /var/lib/matrix/homeserver.yaml; then
  80. sed -i 's|log_config:|#log_config:|g' /var/lib/matrix/homeserver.yaml
  81. fi
  82. fi
  83. if [ -f /etc/fail2ban/fail2ban.conf ]; then
  84. sed -i 's|loglevel.*|loglevel = 3|g' /etc/fail2ban/fail2ban.conf
  85. sed -i 's|logtarget.*|logtarget = /var/log/fail2ban.log|g' /etc/fail2ban/fail2ban.conf
  86. fi
  87. if [ -d /etc/tor ]; then
  88. if [ ! -d /var/log/tor ]; then
  89. mkdir /var/log/tor
  90. chown -R debian-tor:adm /var/log/tor
  91. fi
  92. if [ ! -f /var/log/tor/notices.log ]; then
  93. touch /var/log/tor/notices.log
  94. chown debian-tor:adm /var/log/tor/notices.log
  95. fi
  96. sed -i 's|#Log notice file.*|Log notice file /var/log/tor/notices.log|g' /etc/tor/torrc
  97. sed -i 's|Log notice file.*|Log notice file /var/log/tor/notices.log|g' /etc/tor/torrc
  98. fi
  99. if [ -f /etc/mumble-server.ini ]; then
  100. sed -i 's|logfile=.*|logfile=/var/log/mumble-server.log|g' /etc/mumble-server.ini
  101. fi
  102. if [ -f /etc/php/7.0/fpm/php-fpm.conf ]; then
  103. sed -i 's|error_log =.*|error_log = /var/log/php-fpm.log|g' /etc/php/7.0/fpm/php-fpm.conf
  104. fi
  105. if [ -d /etc/nginx ]; then
  106. if [ ! -d /var/log/nginx ]; then
  107. mkdir /var/log/nginx
  108. fi
  109. for filename in /etc/nginx/sites-available/* ; do
  110. filename_domain=$(echo "$filename" | awk -F '/' '{print $5}')
  111. sed -i "s|access_log.*|access_log /var/log/nginx/$filename_domain.access.log;|g" $filename
  112. sed -i "s|error_log.*|error_log /var/log/nginx/$filename_domain.err.log $WEBSERVER_LOG_LEVEL;|g" $filename
  113. done
  114. sed -i 's|access_log.*|access_log /var/log/nginx/access.log;|g' /etc/nginx/nginx.conf
  115. sed -i 's|error_log.*|error_log /var/log/nginx/error.log;|g' /etc/nginx/nginx.conf
  116. fi
  117. if [ -f /etc/init.d/spamassassin ]; then
  118. sed -i 's|DOPTIONS="-s null -d --pidfile=$PIDFILE"|DOPTIONS="-d --pidfile=$PIDFILE"|g' /etc/init.d/spamassassin
  119. fi
  120. if [ -d /etc/prosody ]; then
  121. if [ ! -d /var/log/prosody ]; then
  122. mkdir /var/log/prosody
  123. chown root:adm /var/log/prosody
  124. fi
  125. sed -i 's|info = "/dev/null";|info = "/var/log/prosody/prosody.log";|g' /etc/prosody/prosody.cfg.lua
  126. sed -i 's|error = "/dev/null";|error = "/var/log/prosody/prosody.err";|g' /etc/prosody/prosody.cfg.lua
  127. sed -i 's|levels = { "error" }; to = "/dev/null";|levels = { "error" }; to = "syslog";|g' /etc/prosody/prosody.cfg.lua
  128. fi
  129. if [ -d /etc/exim4 ]; then
  130. if [ ! -d /var/log/exim4 ]; then
  131. mkdir /var/log/exim4
  132. fi
  133. sed -i 's|MAIN_LOG_SELECTOR = .*|MAIN_LOG_SELECTOR = +tls_peerdn|g' /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs
  134. sed -i 's|MAIN_LOG_SELECTOR = .*|MAIN_LOG_SELECTOR = +tls_peerdn|g' /etc/exim4/exim4.conf.template
  135. sed -i 's|log_selector =.*|log_selector = MAIN_LOG_SELECTOR|g' /etc/exim4/conf.d/main/90_exim4-config_log_selector
  136. fi
  137. if [ -f /etc/dovecot/dovecot.conf ]; then
  138. sed -i 's|log_path =.*|log_path = /var/log/dovecot.log|g' /etc/dovecot/dovecot.conf
  139. sed -i 's|info_log_path =.*|info_log_path = /var/log/dovecot-info.log|g' /etc/dovecot/dovecot.conf
  140. sed -i 's|debug_log_path =.*|debug_log_path = /var/log/dovecot-debug.log|g' /etc/dovecot/dovecot.conf
  141. fi
  142. if [ -d /etc/mysql ]; then
  143. if [ ! -d /var/log/mysql ]; then
  144. mkdir /var/log/mysql
  145. fi
  146. if [ -f /etc/mysql/my.cnf ]; then
  147. sed -i 's|log_error =.*|log_error = /var/log/mysql/error.log|g' /etc/mysql/my.cnf
  148. fi
  149. fi
  150. turn_on_rsys_logging
  151. else
  152. if [ -f /var/lib/matrix/homeserver.yaml ]; then
  153. sed -i 's|log_file:.*|log_file: /dev/null|g' /var/lib/matrix/homeserver.yaml
  154. if ! grep -q "#log_config:" /var/lib/matrix/homeserver.yaml; then
  155. sed -i 's|log_config:|#log_config:|g' /var/lib/matrix/homeserver.yaml
  156. fi
  157. if [ -f /etc/matrix/homeserver.log ]; then
  158. $REMOVE_FILES_COMMAND /etc/matrix/homeserver.log
  159. fi
  160. if [ -f /etc/matrix/homeserver.log.1 ]; then
  161. $REMOVE_FILES_COMMAND /etc/matrix/homeserver.log.1
  162. fi
  163. fi
  164. if [ -d /etc/tor ]; then
  165. sed -i 's|#Log notice file.*|Log notice file /dev/null|g' /etc/tor/torrc
  166. sed -i 's|Log notice file.*|Log notice file /dev/null|g' /etc/tor/torrc
  167. fi
  168. if [ -f /etc/mumble-server.ini ]; then
  169. sed -i 's|logfile=.*|logfile=/dev/null|g' /etc/mumble-server.ini
  170. if [ -d /var/log/mumble-server ]; then
  171. $REMOVE_FILES_COMMAND /var/log/mumble-server/*
  172. rm -rf /var/log/mumble-server
  173. fi
  174. fi
  175. if [ -d /var/log/radicale ]; then
  176. $REMOVE_FILES_COMMAND /var/log/radicale/*
  177. rm -rf /var/log/radicale
  178. fi
  179. if [ -f /etc/php/7.0/fpm/php-fpm.conf ]; then
  180. sed -i 's|error_log =.*|error_log = /dev/null|g' /etc/php/7.0/fpm/php-fpm.conf
  181. $REMOVE_FILES_COMMAND /var/log/php-fpm.*
  182. fi
  183. if [ -d /etc/nginx ]; then
  184. for filename in /etc/nginx/sites-available/* ; do
  185. sed -i 's|access_log.*|access_log /dev/null;|g' $filename
  186. sed -i 's|warn_log.*|warn_log /dev/null;|g' $filename
  187. sed -i 's|error_log.*|error_log /dev/null;|g' $filename
  188. done
  189. sed -i 's|access_log.*|access_log /dev/null;|g' /etc/nginx/nginx.conf
  190. sed -i 's|error_log.*|error_log /dev/null;|g' /etc/nginx/nginx.conf
  191. $REMOVE_FILES_COMMAND /var/log/nginx/*
  192. fi
  193. if [ -f /etc/init.d/spamassassin ]; then
  194. sed -i 's|DOPTIONS="-d --pidfile=$PIDFILE"|DOPTIONS="-s null -d --pidfile=$PIDFILE"|g' /etc/init.d/spamassassin
  195. fi
  196. if [ -d /etc/prosody ]; then
  197. sed -i 's|info = "/var/log/prosody/prosody.log";|info = "/dev/null";|g' /etc/prosody/prosody.cfg.lua
  198. sed -i 's|error = "/var/log/prosody/prosody.err";|error = "/dev/null";|g' /etc/prosody/prosody.cfg.lua
  199. sed -i 's|levels = { "error" }; to = "syslog";|levels = { "error" }; to = "/dev/null";|g' /etc/prosody/prosody.cfg.lua
  200. $REMOVE_FILES_COMMAND /var/log/prosody/*
  201. rm -rf /var/log/prosody
  202. fi
  203. if [ -d /etc/exim4 ]; then
  204. sed -i 's|MAIN_LOG_SELECTOR = .*|MAIN_LOG_SELECTOR = -all|g' /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs
  205. sed -i 's|MAIN_LOG_SELECTOR = .*|MAIN_LOG_SELECTOR = -all|g' /etc/exim4/exim4.conf.template
  206. sed -i 's|log_selector =.*|log_selector = -all|g' /etc/exim4/conf.d/main/90_exim4-config_log_selector
  207. $REMOVE_FILES_COMMAND /var/log/exim4/*
  208. fi
  209. if [ -f /etc/dovecot/dovecot.conf ]; then
  210. sed -i 's|log_path =.*|log_path = /dev/null|g' /etc/dovecot/dovecot.conf
  211. sed -i 's|info_log_path =.*|info_log_path = /dev/null|g' /etc/dovecot/dovecot.conf
  212. sed -i 's|debug_log_path =.*|debug_log_path = /dev/null|g' /etc/dovecot/dovecot.conf
  213. $REMOVE_FILES_COMMAND /var/log/mail.*
  214. $REMOVE_FILES_COMMAND /var/log/dovecot*
  215. fi
  216. if [ -d /etc/mysql ]; then
  217. if [ -d /var/log/mysql ]; then
  218. $REMOVE_FILES_COMMAND /var/log/mysql/*
  219. fi
  220. if [ -f /var/log/mysql.err ]; then
  221. $REMOVE_FILES_COMMAND /var/log/mysql.err
  222. fi
  223. if [ -f /var/log/mysql.log ]; then
  224. $REMOVE_FILES_COMMAND /var/log/mysql.log
  225. fi
  226. if [ -f /etc/mysql/my.cnf ]; then
  227. sed -i 's|log_error =.*|log_error = /dev/null|g' /etc/mysql/my.cnf
  228. fi
  229. fi
  230. if [ -f /etc/fail2ban/fail2ban.conf ]; then
  231. sed -i 's|loglevel.*|loglevel = 1|g' /etc/fail2ban/fail2ban.conf
  232. sed -i 's|logtarget.*|logtarget = /dev/null|g' /etc/fail2ban/fail2ban.conf
  233. $REMOVE_FILES_COMMAND /var/log/fail2ban.*
  234. fi
  235. turn_off_rsys_logging
  236. fi
  237. if [ -d /etc/exim4 ]; then
  238. update-exim4.conf.template -r
  239. update-exim4.conf
  240. dpkg-reconfigure --frontend noninteractive exim4-config
  241. fi
  242. if [[ "$2" == "--reboot"* || "$2" == "--restart"* ]]; then
  243. # if we are rebooting anyway then there is no need to
  244. # restart the daemons
  245. exit 0
  246. fi
  247. if [ -d /etc/exim4 ]; then
  248. systemctl restart exim4
  249. fi
  250. systemctl restart syslog
  251. if [ -d /etc/tor ]; then
  252. if [[ "$2" != "--onion" ]]; then
  253. systemctl restart tor
  254. fi
  255. fi
  256. if [ -d /etc/nginx ]; then
  257. systemctl restart php7.0-fpm
  258. systemctl restart nginx
  259. fi
  260. if [ -f /etc/init.d/spamassassin ]; then
  261. systemctl restart spamassassin
  262. fi
  263. if [ -d /etc/prosody ]; then
  264. systemctl restart prosody
  265. fi
  266. if [ -d /etc/dovecot ]; then
  267. systemctl restart dovecot
  268. fi
  269. if [ -f /etc/mumble-server.ini ]; then
  270. systemctl restart mumble-server
  271. fi
  272. if [ -d /var/www/radicale ]; then
  273. systemctl restart radicale
  274. fi
  275. if [ -d /etc/fail2ban ]; then
  276. systemctl restart fail2ban
  277. fi
  278. if [ -d /etc/matrix ]; then
  279. systemctl restart matrix
  280. fi
  281. exit 0