# snorby-install.sh
#
# Copyright 2012 Silvio Knizek <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
######## requirements ########
# - a running mysql somewhere
# - a user with access to a db
# - more than 16MB in /tmp (256MB works)
# probably you want in mysql
# <<create database snorby;>>
# <<grant all privileges on snorby.* to "snorby"@"%" identified by "secret_password";>>
# You should know what you do if you you need snorby. For everything else is irc://irc.freenode.net/#snorby
# Hint: this script was written and tested on a vServer, because vServer are cool 8-)
echo -n -e "Mail to:\t" ; read MAILTO
echo -n -e "Server name:\t" ; read SRVNAME
echo -n -e "MySQL server:\t" ; read MYSERVER
echo -n -e "MySQL database:\t" ; read MYDB
echo -n -e "MySQL user:\t" ; read MYUSER
echo -n -e "MySQL password:\t" ; read MYPASS
RULES_DIR=/etc/snort/rules
WWW_DIR=/var/www/ # where snorby should be installed
######## install snorby ########
# install run deps
echo "Installing requirements for running snorby. This could take a while..."
aptitude install -y ruby1.9.1 imagemagick wkhtmltopdf openjdk-6-jdk > /dev/null
if [[ $? != 0 ]]; then exit 1; fi
echo "Installing done."
RUBYGEM_BIN=$(gem1.9.1 environment gemdir)/bin
WKHTMLTOPDF=$(which wkhtmltopdf)
# install make deps
echo "Installing requirements for building snorby. This could take a while..."
aptitude install -y ruby1.9.1-dev make unzip git libxml2-dev libxslt1-dev libmysqlclient-dev g++ libmagickcore-dev libmagickwand-dev libcurl4-openssl-dev libssl-dev apache2-prefork-dev libapr1-dev libaprutil1-dev > /dev/null
if [[ $? != 0 ]]; then exit 1; fi
ln -s /usr/bin/ruby1.9.1 /usr/local/bin/ruby # or another way to help /usr/bin/env to find ruby
echo "Install rails framework..."
gem1.9.1 install rails > /dev/null # after all, it's a rail application
if [[ $? != 0 ]]; then exit 1; fi
gem1.9.1 install pdfkit > /dev/null # which can produce pdfs
if [[ $? != 0 ]]; then exit 1; fi
echo "Installing done."
echo "Building snorby in ${WWW_DIR}/snorby..."
mkdir -p ${WWW_DIR}/snorby # the installdir
cd ${WWW_DIR}
echo "Get source..."
wget https://github.com/Snorby/snorby/zipball/v2.5.1 -O snorby.zip > /dev/null # get the snorby source
if [[ $? != 0 ]]; then exit 1; fi
echo "Got source."
unzip snorby.zip > /dev/null
mv Snorby-snorby-83bdd7a/* snorby/ > /dev/null
mv Snorby-snorby-83bdd7a/.gitignore snorby/ > /dev/null
rm -f Snorby-snorby-83bdd7a
rm snorby.zip
chown -R www-data:www-data ${WWW_DIR} # make the snorby source available to apache
cd snorby/config
cp database.example.yml database.yml
cp snorby_config.example.yml snorby_config.yml
echo "Configure snorby..."
# configure snorby database.yml
sed -i "s|username:.*|username: ${MYUSER}|g" database.yml
sed -i "s|password:.*|password: ${MYPASS}|g" database.yml
sed -i "s|host:.*|host: ${MYSERVER}|g" database.yml
sed -i "s|database:.*|database: ${MYDB}|g" database.yml
#configure snorby config.yml
sed -i "s|domain:.*|domain: localhost:3000|g" snorby_config.yml
sed -i "s|wkhtmltopdf:.*|wkhtmltopdf: ${WKHTMLTOPDF}|g" snorby_config.yml
sed -i "s|mailer_sender:.*|mailer_sender: '${MAILTO}'|g" snorby_config.yml
sed -i 's| - ""| - "/etc/snort/rules"|g' snorby_config.yml
echo "Configuring done."
# final install
echo "Installing snorby..."
${RUBYGEM_BIN}/bundle install > /dev/null
${RUBYGEM_BIN}/bundle exec rake snorby:setup > /dev/null
if [[ $? != 0 ]]; then exit 1; fi
echo "Installing snorby done."
echo -n -e "\n\n\n"
######## install apache2 ########
echo "Installing and setting up Apache..."
aptitude install -y apache2 > /dev/null
gem1.9.1 install passenger > /dev/null
/var/lib/gems/1.9.1/gems/passenger-3.0.14/bin/passenger-install-apache2-module -a > /dev/null
if [[ $? != 0 ]]; then exit 1; fi
echo "LoadModule passenger_module /var/lib/gems/1.9.1/gems/passenger-3.0.14/ext/apache2/mod_passenger.so" > /etc/apache2/mods-available/passenger.load
echo -e "PassengerRoot /var/lib/gems/1.9.1/gems/passenger-3.0.14\nPassengerRuby /usr/bin/ruby1.9.1" > /etc/apache2/mods-available/passenger.conf
a2enmod passenger > /dev/null
echo -e "<VirtualHost *:80>\n ServerAdmin ${MAILTO}\n ServerName ${SRVNAME}\n DocumentRoot ${WWW_DIR}/snorby/public\n\n <Directory \"${WWW_DIR}/snorby/public\">\n AllowOverride all\n Options -MultiViews\n </Directory>\n\n</VirtualHost>\n\nPassengerPreStart http://${SRVNAME}" > /etc/apache2/sites-available/snorby
a2dissite default > /dev/null
a2ensite snorby > /dev/null
/etc/init.d/apache2 restart > /dev/null
echo "Apache done."
echo "Clean environment..."
unset MAILTO SRVNAME MYSERVER MYDB MYUSER MYPASS RULES_DIR WWW_DIR WKHTMLTOPDF
echo "Mark all build dependencies as automatically installed..."
aptitude markauto ruby1.9.1-dev make unzip git libxml2-dev libxslt1-dev libmysqlclient-dev g++ libmagickcore-dev libmagickwand-dev libcurl4-openssl-dev libssl-dev apache2-prefork-dev libapr1-dev libaprutil1-dev
echo "Quit. Have a nice day."