freedombone-logging 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. APP_FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  38. for f in $APP_FILES
  39. do
  40. source $f
  41. done
  42. APPS_AVAILABLE=()
  43. function logging_get_app_names {
  44. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  45. for filename in $FILES
  46. do
  47. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  48. if grep -q "logging_on_" ${filename}; then
  49. if grep -q "logging_off_" ${filename}; then
  50. APPS_AVAILABLE+=("${app_name}")
  51. fi
  52. fi
  53. done
  54. }
  55. function turn_logging_on {
  56. logging_get_app_names
  57. for a in "${APPS_AVAILABLE[@]}"
  58. do
  59. echo $"Turning on logging for ${a}"
  60. logging_on_${a}
  61. done
  62. }
  63. function turn_logging_off {
  64. logging_get_app_names
  65. for a in "${APPS_AVAILABLE[@]}"
  66. do
  67. echo $"Turning off logging for ${a}"
  68. logging_off_${a}
  69. done
  70. }
  71. function turn_off_rsys_logging {
  72. sed -i 's|mail,news.none.*|mail,news.none /dev/null|g' /etc/rsyslog.conf
  73. sed -i 's|auth,authpriv.\*.*|auth,authpriv.\* /dev/null|g' /etc/rsyslog.conf
  74. sed -i 's|mail.info.*|mail.info /dev/null|g' /etc/rsyslog.conf
  75. sed -i 's|mail.warn.*|mail.warn /dev/null|g' /etc/rsyslog.conf
  76. sed -i 's|mail.err.*|mail.err /dev/null|g' /etc/rsyslog.conf
  77. sed -i 's|daemon.\*.*|daemon.\* /dev/null|g' /etc/rsyslog.conf
  78. sed -i 's|mail.\*.*|mail.\* /dev/null|g' /etc/rsyslog.conf
  79. sed -i 's|user.\*.*|user.\* /dev/null|g' /etc/rsyslog.conf
  80. sed -i 's|news.none;mail.none.*|news.none;mail.none /dev/null|g' /etc/rsyslog.conf
  81. sed -i 's|\*.\*;auth,authpriv.none.*|\*.\*;auth,authpriv.none /dev/null|g' /etc/rsyslog.conf
  82. sed -i 's|#cron.\*|cron.\*|g' /etc/rsyslog.conf
  83. sed -i 's|cron.\*.*|cron.\* /dev/null|g' /etc/rsyslog.conf
  84. $REMOVE_FILES_COMMAND /var/log/wtmp*
  85. $REMOVE_FILES_COMMAND /var/log/debug*
  86. $REMOVE_FILES_COMMAND /var/log/cron.*
  87. $REMOVE_FILES_COMMAND /var/log/auth.*
  88. $REMOVE_FILES_COMMAND /var/log/mail.*
  89. $REMOVE_FILES_COMMAND /var/log/daemon.*
  90. $REMOVE_FILES_COMMAND /var/log/user.*
  91. $REMOVE_FILES_COMMAND /var/log/messages*
  92. }
  93. function turn_on_rsys_logging {
  94. sed -i 's|mail,news.none.*|mail,news.none -/var/log/messages|g' /etc/rsyslog.conf
  95. sed -i 's|auth,authpriv.\*.*|auth,authpriv.\* /var/log/auth.log|g' /etc/rsyslog.conf
  96. sed -i 's|mail.info.*|mail.info -/var/log/mail.info|g' /etc/rsyslog.conf
  97. sed -i 's|mail.warn.*|mail.warn -/var/log/mail.warn|g' /etc/rsyslog.conf
  98. sed -i 's|mail.err.*|mail.err /var/log/mail.err|g' /etc/rsyslog.conf
  99. sed -i 's|daemon.\*.*|daemon.\* -/var/log/daemon.log|g' /etc/rsyslog.conf
  100. sed -i 's|mail.\*.*|mail.\* -/var/log/mail.log|g' /etc/rsyslog.conf
  101. sed -i 's|user.\*.*|user.\* -/var/log/user.log|g' /etc/rsyslog.conf
  102. sed -i 's|news.none;mail.none.*|news.none;mail.none -/var/log/debug|g' /etc/rsyslog.conf
  103. sed -i 's|\*.\*;auth,authpriv.none.*|\*.\*;auth,authpriv.none -/var/log/syslog|g' /etc/rsyslog.conf
  104. sed -i 's|#cron.\*|cron.\*|g' /etc/rsyslog.conf
  105. sed -i 's|cron.\*.*|cron.\* /var/log/cron.log|g' /etc/rsyslog.conf
  106. }
  107. if [ ! "$1" ]; then
  108. exit 1
  109. fi
  110. if [[ "$1" == "on" || "$1" == "On" || "$1" == "ON" ]]; then
  111. turn_logging_on
  112. if [ -f /etc/fail2ban/fail2ban.conf ]; then
  113. sed -i 's|loglevel.*|loglevel = 3|g' /etc/fail2ban/fail2ban.conf
  114. sed -i 's|logtarget.*|logtarget = /var/log/fail2ban.log|g' /etc/fail2ban/fail2ban.conf
  115. fi
  116. if [ -d /etc/tor ]; then
  117. if [ ! -d /var/log/tor ]; then
  118. mkdir /var/log/tor
  119. chown -R debian-tor:adm /var/log/tor
  120. fi
  121. if [ ! -f /var/log/tor/notices.log ]; then
  122. touch /var/log/tor/notices.log
  123. chown debian-tor:adm /var/log/tor/notices.log
  124. fi
  125. sed -i 's|#Log notice file.*|Log notice file /var/log/tor/notices.log|g' /etc/tor/torrc
  126. sed -i 's|Log notice file.*|Log notice file /var/log/tor/notices.log|g' /etc/tor/torrc
  127. fi
  128. if [ -f /etc/php/7.0/fpm/php-fpm.conf ]; then
  129. sed -i 's|error_log =.*|error_log = /var/log/php-fpm.log|g' /etc/php/7.0/fpm/php-fpm.conf
  130. fi
  131. if [ -d /etc/nginx ]; then
  132. if [ ! -d /var/log/nginx ]; then
  133. mkdir /var/log/nginx
  134. fi
  135. for filename in /etc/nginx/sites-available/* ; do
  136. filename_domain=$(echo "$filename" | awk -F '/' '{print $5}')
  137. sed -i "s|access_log.*|access_log /var/log/nginx/$filename_domain.access.log;|g" $filename
  138. sed -i "s|error_log.*|error_log /var/log/nginx/$filename_domain.err.log $WEBSERVER_LOG_LEVEL;|g" $filename
  139. done
  140. sed -i 's|access_log.*|access_log /var/log/nginx/access.log;|g' /etc/nginx/nginx.conf
  141. sed -i 's|error_log.*|error_log /var/log/nginx/error.log;|g' /etc/nginx/nginx.conf
  142. fi
  143. if [ -f /etc/init.d/spamassassin ]; then
  144. sed -i 's|DOPTIONS="-s null -d --pidfile=$PIDFILE"|DOPTIONS="-d --pidfile=$PIDFILE"|g' /etc/init.d/spamassassin
  145. fi
  146. if [ -d /etc/exim4 ]; then
  147. if [ ! -d /var/log/exim4 ]; then
  148. mkdir /var/log/exim4
  149. fi
  150. sed -i 's|MAIN_LOG_SELECTOR = .*|MAIN_LOG_SELECTOR = +tls_peerdn|g' /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs
  151. sed -i 's|MAIN_LOG_SELECTOR = .*|MAIN_LOG_SELECTOR = +tls_peerdn|g' /etc/exim4/exim4.conf.template
  152. sed -i 's|log_selector =.*|log_selector = MAIN_LOG_SELECTOR|g' /etc/exim4/conf.d/main/90_exim4-config_log_selector
  153. fi
  154. if [ -f /etc/dovecot/dovecot.conf ]; then
  155. sed -i 's|log_path =.*|log_path = /var/log/dovecot.log|g' /etc/dovecot/dovecot.conf
  156. sed -i 's|info_log_path =.*|info_log_path = /var/log/dovecot-info.log|g' /etc/dovecot/dovecot.conf
  157. sed -i 's|debug_log_path =.*|debug_log_path = /var/log/dovecot-debug.log|g' /etc/dovecot/dovecot.conf
  158. fi
  159. if [ -d /etc/mysql ]; then
  160. if [ ! -d /var/log/mysql ]; then
  161. mkdir /var/log/mysql
  162. fi
  163. if [ -f /etc/mysql/my.cnf ]; then
  164. sed -i 's|log_error =.*|log_error = /var/log/mysql/error.log|g' /etc/mysql/my.cnf
  165. fi
  166. fi
  167. turn_on_rsys_logging
  168. else
  169. turn_logging_off
  170. if [ -d /etc/tor ]; then
  171. sed -i 's|#Log notice file.*|Log notice file /dev/null|g' /etc/tor/torrc
  172. sed -i 's|Log notice file.*|Log notice file /dev/null|g' /etc/tor/torrc
  173. fi
  174. if [ -d /var/log/radicale ]; then
  175. $REMOVE_FILES_COMMAND /var/log/radicale/*
  176. rm -rf /var/log/radicale
  177. fi
  178. if [ -f /etc/php/7.0/fpm/php-fpm.conf ]; then
  179. sed -i 's|error_log =.*|error_log = /dev/null|g' /etc/php/7.0/fpm/php-fpm.conf
  180. $REMOVE_FILES_COMMAND /var/log/php-fpm.*
  181. fi
  182. if [ -d /etc/nginx ]; then
  183. for filename in /etc/nginx/sites-available/* ; do
  184. sed -i 's|access_log.*|access_log /dev/null;|g' $filename
  185. sed -i 's|warn_log.*|warn_log /dev/null;|g' $filename
  186. sed -i 's|error_log.*|error_log /dev/null;|g' $filename
  187. done
  188. sed -i 's|access_log.*|access_log /dev/null;|g' /etc/nginx/nginx.conf
  189. sed -i 's|error_log.*|error_log /dev/null;|g' /etc/nginx/nginx.conf
  190. $REMOVE_FILES_COMMAND /var/log/nginx/*
  191. fi
  192. if [ -f /etc/init.d/spamassassin ]; then
  193. sed -i 's|DOPTIONS="-d --pidfile=$PIDFILE"|DOPTIONS="-s null -d --pidfile=$PIDFILE"|g' /etc/init.d/spamassassin
  194. fi
  195. if [ -d /etc/exim4 ]; then
  196. sed -i 's|MAIN_LOG_SELECTOR = .*|MAIN_LOG_SELECTOR = -all|g' /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs
  197. sed -i 's|MAIN_LOG_SELECTOR = .*|MAIN_LOG_SELECTOR = -all|g' /etc/exim4/exim4.conf.template
  198. sed -i 's|log_selector =.*|log_selector = -all|g' /etc/exim4/conf.d/main/90_exim4-config_log_selector
  199. $REMOVE_FILES_COMMAND /var/log/exim4/*
  200. fi
  201. if [ -f /etc/dovecot/dovecot.conf ]; then
  202. sed -i 's|log_path =.*|log_path = /dev/null|g' /etc/dovecot/dovecot.conf
  203. sed -i 's|info_log_path =.*|info_log_path = /dev/null|g' /etc/dovecot/dovecot.conf
  204. sed -i 's|debug_log_path =.*|debug_log_path = /dev/null|g' /etc/dovecot/dovecot.conf
  205. $REMOVE_FILES_COMMAND /var/log/mail.*
  206. $REMOVE_FILES_COMMAND /var/log/dovecot*
  207. fi
  208. if [ -d /etc/mysql ]; then
  209. if [ -d /var/log/mysql ]; then
  210. $REMOVE_FILES_COMMAND /var/log/mysql/*
  211. fi
  212. if [ -f /var/log/mysql.err ]; then
  213. $REMOVE_FILES_COMMAND /var/log/mysql.err
  214. fi
  215. if [ -f /var/log/mysql.log ]; then
  216. $REMOVE_FILES_COMMAND /var/log/mysql.log
  217. fi
  218. if [ -f /etc/mysql/my.cnf ]; then
  219. sed -i 's|log_error =.*|log_error = /dev/null|g' /etc/mysql/my.cnf
  220. fi
  221. fi
  222. if [ -f /etc/fail2ban/fail2ban.conf ]; then
  223. sed -i 's|loglevel.*|loglevel = 1|g' /etc/fail2ban/fail2ban.conf
  224. sed -i 's|logtarget.*|logtarget = /dev/null|g' /etc/fail2ban/fail2ban.conf
  225. $REMOVE_FILES_COMMAND /var/log/fail2ban.*
  226. fi
  227. turn_off_rsys_logging
  228. fi
  229. if [ -d /etc/exim4 ]; then
  230. update-exim4.conf.template -r
  231. update-exim4.conf
  232. dpkg-reconfigure --frontend noninteractive exim4-config
  233. fi
  234. if [[ "$2" == "--reboot"* || "$2" == "--restart"* ]]; then
  235. # if we are rebooting anyway then there is no need to
  236. # restart the daemons
  237. exit 0
  238. fi
  239. if [ -d /etc/exim4 ]; then
  240. systemctl restart exim4
  241. fi
  242. systemctl restart syslog
  243. if [ -d /etc/tor ]; then
  244. if [[ "$2" != "--onion" ]]; then
  245. systemctl restart tor
  246. fi
  247. fi
  248. if [ -d /etc/nginx ]; then
  249. systemctl restart php7.0-fpm
  250. systemctl restart nginx
  251. fi
  252. if [ -f /etc/init.d/spamassassin ]; then
  253. systemctl restart spamassassin
  254. fi
  255. if [ -d /etc/prosody ]; then
  256. systemctl restart prosody
  257. fi
  258. if [ -d /etc/dovecot ]; then
  259. systemctl restart dovecot
  260. fi
  261. if [ -f /etc/mumble-server.ini ]; then
  262. systemctl restart mumble-server
  263. fi
  264. if [ -d /var/www/radicale ]; then
  265. systemctl restart radicale
  266. fi
  267. if [ -d /etc/fail2ban ]; then
  268. systemctl restart fail2ban
  269. fi
  270. if [ -d /etc/matrix ]; then
  271. systemctl restart matrix
  272. fi
  273. exit 0