freedombone-logging 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. function turn_off_rsys_logging {
  34. sed -i 's|mail,news.none.*|mail,news.none /dev/null|g' /etc/rsyslog.conf
  35. sed -i 's|auth,authpriv.\*.*|auth,authpriv.\* /dev/null|g' /etc/rsyslog.conf
  36. sed -i 's|mail.info.*|mail.info /dev/null|g' /etc/rsyslog.conf
  37. sed -i 's|mail.warn.*|mail.warn /dev/null|g' /etc/rsyslog.conf
  38. sed -i 's|mail.err.*|mail.err /dev/null|g' /etc/rsyslog.conf
  39. sed -i 's|daemon.\*.*|daemon.\* /dev/null|g' /etc/rsyslog.conf
  40. sed -i 's|mail.\*.*|mail.\* /dev/null|g' /etc/rsyslog.conf
  41. sed -i 's|user.\*.*|user.\* /dev/null|g' /etc/rsyslog.conf
  42. sed -i 's|news.none;mail.none.*|news.none;mail.none /dev/null|g' /etc/rsyslog.conf
  43. sed -i 's|\*.\*;auth,authpriv.none.*|\*.\*;auth,authpriv.none /dev/null|g' /etc/rsyslog.conf
  44. sed -i 's|#cron.\*|cron.\*|g' /etc/rsyslog.conf
  45. sed -i 's|cron.\*.*|cron.\* /dev/null|g' /etc/rsyslog.conf
  46. shred -zu /var/log/wtmp*
  47. shred -zu /var/log/debug*
  48. shred -zu /var/log/cron.*
  49. shred -zu /var/log/auth.*
  50. shred -zu /var/log/mail.*
  51. shred -zu /var/log/daemon.*
  52. shred -zu /var/log/user.*
  53. shred -zu /var/log/messages*
  54. }
  55. function turn_on_rsys_logging {
  56. sed -i 's|mail,news.none.*|mail,news.none -/var/log/messages|g' /etc/rsyslog.conf
  57. sed -i 's|auth,authpriv.\*.*|auth,authpriv.\* /var/log/auth.log|g' /etc/rsyslog.conf
  58. sed -i 's|mail.info.*|mail.info -/var/log/mail.info|g' /etc/rsyslog.conf
  59. sed -i 's|mail.warn.*|mail.warn -/var/log/mail.warn|g' /etc/rsyslog.conf
  60. sed -i 's|mail.err.*|mail.err /var/log/mail.err|g' /etc/rsyslog.conf
  61. sed -i 's|daemon.\*.*|daemon.\* -/var/log/daemon.log|g' /etc/rsyslog.conf
  62. sed -i 's|mail.\*.*|mail.\* -/var/log/mail.log|g' /etc/rsyslog.conf
  63. sed -i 's|user.\*.*|user.\* -/var/log/user.log|g' /etc/rsyslog.conf
  64. sed -i 's|news.none;mail.none.*|news.none;mail.none -/var/log/debug|g' /etc/rsyslog.conf
  65. sed -i 's|\*.\*;auth,authpriv.none.*|\*.\*;auth,authpriv.none -/var/log/syslog|g' /etc/rsyslog.conf
  66. sed -i 's|#cron.\*|cron.\*|g' /etc/rsyslog.conf
  67. sed -i 's|cron.\*.*|cron.\* /var/log/cron.log|g' /etc/rsyslog.conf
  68. }
  69. if [ ! "$1" ]; then
  70. exit 1
  71. fi
  72. if [[ "$1" == "on" || "$1" == "On" || "$1" == "ON" ]]; then
  73. if [ -f /etc/fail2ban/fail2ban.conf ]; then
  74. sed -i 's|loglevel.*|loglevel = 3|g' /etc/fail2ban/fail2ban.conf
  75. sed -i 's|logtarget.*|logtarget = /var/log/fail2ban.log|g' /etc/fail2ban/fail2ban.conf
  76. fi
  77. if [ -d /etc/tor ]; then
  78. if [ ! -f /var/log/tor.log ]; then
  79. touch /var/log/tor.log
  80. chown root:debian-tor /var/log/tor.log
  81. fi
  82. sed -i 's|#Log notice file.*|Log notice file /var/log/tor.log|g' /etc/tor/torrc
  83. sed -i 's|Log notice file.*|Log notice file /var/log/tor.log|g' /etc/tor/torrc
  84. fi
  85. if [ -f /etc/mumble-server.ini ]; then
  86. sed -i 's|logfile=.*|logfile=/var/log/mumble-server.log|g' /etc/mumble-server.ini
  87. fi
  88. if [ -f /etc/php5/fpm/php-fpm.conf ]; then
  89. sed -i 's|error_log =.*|error_log = /var/log/php5-fpm.log|g' /etc/php5/fpm/php-fpm.conf
  90. fi
  91. if [ -d /etc/nginx ]; then
  92. if [ ! -d /var/log/nginx ]; then
  93. mkdir /var/log/nginx
  94. fi
  95. for filename in /etc/nginx/sites-available/* ; do
  96. filename_domain=$(echo "$filename" | awk -F '/' '{print $5}')
  97. sed -i "s|access_log.*|access_log /var/log/nginx/$filename_domain.access.log;|g" $filename
  98. sed -i "s|error_log.*|error_log /var/log/nginx/$filename_domain.err.log $WEBSERVER_LOG_LEVEL;|g" $filename
  99. done
  100. sed -i 's|access_log.*|access_log /var/log/nginx/access.log;|g' /etc/nginx/nginx.conf
  101. sed -i 's|error_log.*|error_log /var/log/nginx/error.log;|g' /etc/nginx/nginx.conf
  102. fi
  103. if [ -f /etc/init.d/spamassassin ]; then
  104. sed -i 's|DOPTIONS="-s null -d --pidfile=$PIDFILE"|DOPTIONS="-d --pidfile=$PIDFILE"|g' /etc/init.d/spamassassin
  105. fi
  106. if [ -d /etc/prosody ]; then
  107. if [ ! -d /var/log/prosody ]; then
  108. mkdir /var/log/prosody
  109. chown root:adm /var/log/prosody
  110. fi
  111. sed -i 's|info = "/dev/null";|info = "/var/log/prosody/prosody.log";|g' /etc/prosody/prosody.cfg.lua
  112. sed -i 's|error = "/dev/null";|error = "/var/log/prosody/prosody.err";|g' /etc/prosody/prosody.cfg.lua
  113. sed -i 's|levels = { "error" }; to = "/dev/null";|levels = { "error" }; to = "syslog";|g' /etc/prosody/prosody.cfg.lua
  114. fi
  115. if [ -d /etc/exim4 ]; then
  116. if [ ! -d /var/log/exim4 ]; then
  117. mkdir /var/log/exim4
  118. fi
  119. sed -i 's|log_selector =.*|log_selector = MAIN_LOG_SELECTOR|g' /etc/exim4/conf.d/main/90_exim4-config_log_selector
  120. fi
  121. if [ -f /etc/dovecot/dovecot.conf ]; then
  122. sed -i 's|log_path =.*|log_path = /var/log/dovecot.log|g' /etc/dovecot/dovecot.conf
  123. sed -i 's|info_log_path =.*|info_log_path = /var/log/dovecot-info.log|g' /etc/dovecot/dovecot.conf
  124. sed -i 's|debug_log_path =.*|debug_log_path = /var/log/dovecot-debug.log|g' /etc/dovecot/dovecot.conf
  125. fi
  126. if [ -d /etc/mysql ]; then
  127. if [ ! -d /var/log/mysql ]; then
  128. mkdir /var/log/mysql
  129. fi
  130. sed -i 's|log_error =.*|log_error = /var/log/mysql/error.log|g' /etc/mysql/my.cnf
  131. fi
  132. turn_on_rsys_logging
  133. else
  134. if [ -d /etc/tor ]; then
  135. sed -i 's|#Log notice file.*|Log notice file /dev/null|g' /etc/tor/torrc
  136. sed -i 's|Log notice file.*|Log notice file /dev/null|g' /etc/tor/torrc
  137. if [ -d /var/log/tor ]; then
  138. shred -zu /var/log/tor/*
  139. rm -rf /var/log/tor
  140. fi
  141. fi
  142. if [ -f /etc/mumble-server.ini ]; then
  143. sed -i 's|logfile=.*|logfile=/dev/null|g' /etc/mumble-server.ini
  144. if [ -d /var/log/mumble-server ]; then
  145. shred -zu /var/log/mumble-server/*
  146. rm -rf /var/log/mumble-server
  147. fi
  148. fi
  149. if [ -d /var/log/radicale ]; then
  150. shred -zu /var/log/radicale/*
  151. rm -rf /var/log/radicale
  152. fi
  153. if [ -f /etc/php5/fpm/php-fpm.conf ]; then
  154. sed -i 's|error_log =.*|error_log = /dev/null|g' /etc/php5/fpm/php-fpm.conf
  155. shred -zu /var/log/php5-fpm.*
  156. fi
  157. if [ -d /etc/nginx ]; then
  158. for filename in /etc/nginx/sites-available/* ; do
  159. sed -i 's|access_log.*|access_log /dev/null;|g' $filename
  160. sed -i 's|warn_log.*|warn_log /dev/null;|g' $filename
  161. sed -i 's|error_log.*|error_log /dev/null;|g' $filename
  162. done
  163. sed -i 's|access_log.*|access_log /dev/null;|g' /etc/nginx/nginx.conf
  164. sed -i 's|error_log.*|error_log /dev/null;|g' /etc/nginx/nginx.conf
  165. shred -zu /var/log/nginx/*
  166. fi
  167. if [ -f /etc/init.d/spamassassin ]; then
  168. sed -i 's|DOPTIONS="-d --pidfile=$PIDFILE"|DOPTIONS="-s null -d --pidfile=$PIDFILE"|g' /etc/init.d/spamassassin
  169. fi
  170. if [ -d /etc/prosody ]; then
  171. sed -i 's|info = "/var/log/prosody/prosody.log";|info = "/dev/null";|g' /etc/prosody/prosody.cfg.lua
  172. sed -i 's|error = "/var/log/prosody/prosody.err";|error = "/dev/null";|g' /etc/prosody/prosody.cfg.lua
  173. sed -i 's|levels = { "error" }; to = "syslog";|levels = { "error" }; to = "/dev/null";|g' /etc/prosody/prosody.cfg.lua
  174. shred -zu /var/log/prosody/*
  175. rm -rf /var/log/prosody
  176. fi
  177. if [ -d /etc/exim4 ]; then
  178. sed -i 's|log_selector =.*|log_selector = -all|g' /etc/exim4/conf.d/main/90_exim4-config_log_selector
  179. shred -zu /var/log/exim4/*
  180. fi
  181. if [ -f /etc/dovecot/dovecot.conf ]; then
  182. sed -i 's|log_path =.*|log_path = /dev/null|g' /etc/dovecot/dovecot.conf
  183. sed -i 's|info_log_path =.*|info_log_path = /dev/null|g' /etc/dovecot/dovecot.conf
  184. sed -i 's|debug_log_path =.*|debug_log_path = /dev/null|g' /etc/dovecot/dovecot.conf
  185. shred -zu /var/log/mail.*
  186. shred -zu /var/log/dovecot*
  187. fi
  188. if [ -d /etc/mysql ]; then
  189. if [ -d /var/log/mysql ]; then
  190. shred -zu /var/log/mysql/*
  191. fi
  192. if [ -f /var/log/mysql.err ]; then
  193. shred -zu /var/log/mysql.err
  194. fi
  195. if [ -f /var/log/mysql.log ]; then
  196. shred -zu /var/log/mysql.log
  197. fi
  198. sed -i 's|log_error =.*|log_error = /dev/null|g' /etc/mysql/my.cnf
  199. fi
  200. if [ -f /etc/fail2ban/fail2ban.conf ]; then
  201. sed -i 's|loglevel.*|loglevel = 1|g' /etc/fail2ban/fail2ban.conf
  202. sed -i 's|logtarget.*|logtarget = /dev/null|g' /etc/fail2ban/fail2ban.conf
  203. shred -zu /var/log/fail2ban.*
  204. fi
  205. turn_off_rsys_logging
  206. fi
  207. systemctl restart syslog
  208. if [ -d /etc/tor ]; then
  209. if [[ "$2" != "--onion" ]]; then
  210. systemctl restart tor
  211. fi
  212. fi
  213. if [ -d /etc/nginx ]; then
  214. systemctl restart php5-fpm
  215. systemctl restart nginx
  216. fi
  217. if [ -f /etc/init.d/spamassassin ]; then
  218. systemctl restart spamassassin
  219. fi
  220. if [ -d /etc/prosody ]; then
  221. systemctl restart prosody
  222. fi
  223. if [ -d /etc/exim4 ]; then
  224. systemctl restart exim4
  225. fi
  226. if [ -d /etc/dovecot ]; then
  227. systemctl restart dovecot
  228. fi
  229. if [ -f /etc/mumble-server.ini ]; then
  230. systemctl restart mumble-server
  231. fi
  232. if [ -d /var/www/radicale ]; then
  233. systemctl restart radicale
  234. fi
  235. if [ -d /etc/fail2ban ]; then
  236. systemctl restart fail2ban
  237. fi
  238. exit 0