freedombone-logging 12KB

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