2019-11-25 22:58:18 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2022-11-30 17:42:16 +01:00
|
|
|
term_proc() {
|
|
|
|
echo "Entrypoint NGINX caught SIGTERM signal!"
|
|
|
|
echo "Killing process $master_pid"
|
|
|
|
kill -TERM "$master_pid" 2>/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
trap term_proc SIGTERM
|
|
|
|
|
2019-11-25 22:58:18 +01:00
|
|
|
[ -z "$MYSQL_HOST" ] && MYSQL_HOST=db
|
|
|
|
[ -z "$MYSQL_PORT" ] && MYSQL_PORT=3306
|
|
|
|
[ -z "$MYSQL_USER" ] && MYSQL_USER=misp
|
|
|
|
[ -z "$MYSQL_PASSWORD" ] && MYSQL_PASSWORD=example
|
|
|
|
[ -z "$MYSQL_DATABASE" ] && MYSQL_DATABASE=misp
|
2023-05-15 13:13:25 +02:00
|
|
|
[ -z "$MYSQLCMD" ] && export MYSQLCMD="mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -P $MYSQL_PORT -h $MYSQL_HOST -r -N $MYSQL_DATABASE"
|
2023-05-17 15:07:21 +02:00
|
|
|
[ -z "$CRON_USER_ID" ] && export CRON_USER_ID="1"
|
|
|
|
[ -z "$HOSTNAME" ] && export HOSTNAME="https://localhost"
|
2023-09-25 22:40:13 +02:00
|
|
|
[ -z "$DISABLE_IPV6" ] && export DISABLE_IPV6=false
|
2020-01-29 20:47:26 +01:00
|
|
|
|
2019-11-28 23:46:30 +01:00
|
|
|
init_mysql(){
|
2019-11-25 22:58:18 +01:00
|
|
|
# Test when MySQL is ready....
|
|
|
|
# wait for Database come ready
|
|
|
|
isDBup () {
|
|
|
|
echo "SHOW STATUS" | $MYSQLCMD 1>/dev/null
|
|
|
|
echo $?
|
|
|
|
}
|
2019-11-28 23:46:30 +01:00
|
|
|
|
2020-07-27 17:38:25 +02:00
|
|
|
isDBinitDone () {
|
|
|
|
# Table attributes has existed since at least v2.1
|
|
|
|
echo "DESCRIBE attributes" | $MYSQLCMD 1>/dev/null
|
|
|
|
echo $?
|
|
|
|
}
|
|
|
|
|
2019-11-25 22:58:18 +01:00
|
|
|
RETRY=100
|
|
|
|
until [ $(isDBup) -eq 0 ] || [ $RETRY -le 0 ] ; do
|
2023-04-13 16:02:02 +02:00
|
|
|
echo "... waiting for database to come up"
|
2019-11-25 22:58:18 +01:00
|
|
|
sleep 5
|
2020-02-12 15:37:23 +01:00
|
|
|
RETRY=$(( RETRY - 1))
|
2019-11-25 22:58:18 +01:00
|
|
|
done
|
|
|
|
if [ $RETRY -le 0 ]; then
|
2023-04-13 16:02:02 +02:00
|
|
|
>&2 echo "... error: Could not connect to Database on $MYSQL_HOST:$MYSQL_PORT"
|
2019-11-25 22:58:18 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-07-27 17:38:25 +02:00
|
|
|
if [ $(isDBinitDone) -eq 0 ]; then
|
2023-04-13 16:02:02 +02:00
|
|
|
echo "... database has already been initialized"
|
2020-07-27 17:38:25 +02:00
|
|
|
else
|
2023-04-13 16:02:02 +02:00
|
|
|
echo "... database has not been initialized, importing MySQL scheme..."
|
2020-07-27 17:38:25 +02:00
|
|
|
$MYSQLCMD < /var/www/MISP/INSTALL/MYSQL.sql
|
|
|
|
fi
|
2019-11-25 22:58:18 +01:00
|
|
|
}
|
|
|
|
|
2023-04-13 16:02:02 +02:00
|
|
|
init_misp_data_files(){
|
|
|
|
# Init config (shared with host)
|
2023-05-15 13:13:25 +02:00
|
|
|
echo "... initialize configuration files"
|
2023-04-13 16:02:02 +02:00
|
|
|
MISP_APP_CONFIG_PATH=/var/www/MISP/app/Config
|
2023-10-16 11:20:00 +02:00
|
|
|
# workaround for https://forums.docker.com/t/sed-couldnt-open-temporary-file-xyz-permission-denied-when-using-virtiofs/125473
|
|
|
|
# [ -f $MISP_APP_CONFIG_PATH/bootstrap.php ] || cp $MISP_APP_CONFIG_PATH.dist/bootstrap.default.php $MISP_APP_CONFIG_PATH/bootstrap.php
|
|
|
|
# [ -f $MISP_APP_CONFIG_PATH/database.php ] || cp $MISP_APP_CONFIG_PATH.dist/database.default.php $MISP_APP_CONFIG_PATH/database.php
|
|
|
|
# [ -f $MISP_APP_CONFIG_PATH/core.php ] || cp $MISP_APP_CONFIG_PATH.dist/core.default.php $MISP_APP_CONFIG_PATH/core.php
|
|
|
|
# [ -f $MISP_APP_CONFIG_PATH/config.php ] || cp $MISP_APP_CONFIG_PATH.dist/config.default.php $MISP_APP_CONFIG_PATH/config.php
|
|
|
|
# [ -f $MISP_APP_CONFIG_PATH/email.php ] || cp $MISP_APP_CONFIG_PATH.dist/email.php $MISP_APP_CONFIG_PATH/email.php
|
|
|
|
# [ -f $MISP_APP_CONFIG_PATH/routes.php ] || cp $MISP_APP_CONFIG_PATH.dist/routes.php $MISP_APP_CONFIG_PATH/routes.php
|
|
|
|
[ -f $MISP_APP_CONFIG_PATH/bootstrap.php ] || dd if=$MISP_APP_CONFIG_PATH.dist/bootstrap.default.php of=$MISP_APP_CONFIG_PATH/bootstrap.php
|
|
|
|
[ -f $MISP_APP_CONFIG_PATH/database.php ] || dd if=$MISP_APP_CONFIG_PATH.dist/database.default.php of=$MISP_APP_CONFIG_PATH/database.php
|
|
|
|
[ -f $MISP_APP_CONFIG_PATH/core.php ] || dd if=$MISP_APP_CONFIG_PATH.dist/core.default.php of=$MISP_APP_CONFIG_PATH/core.php
|
|
|
|
[ -f $MISP_APP_CONFIG_PATH/config.php ] || dd if=$MISP_APP_CONFIG_PATH.dist/config.default.php of=$MISP_APP_CONFIG_PATH/config.php
|
|
|
|
[ -f $MISP_APP_CONFIG_PATH/email.php ] || dd if=$MISP_APP_CONFIG_PATH.dist/email.php of=$MISP_APP_CONFIG_PATH/email.php
|
|
|
|
[ -f $MISP_APP_CONFIG_PATH/routes.php ] || dd if=$MISP_APP_CONFIG_PATH.dist/routes.php of=$MISP_APP_CONFIG_PATH/routes.php
|
|
|
|
|
2023-05-15 13:13:25 +02:00
|
|
|
echo "... initialize database.php settings"
|
2023-10-16 11:20:00 +02:00
|
|
|
# workaround for https://forums.docker.com/t/sed-couldnt-open-temporary-file-xyz-permission-denied-when-using-virtiofs/125473
|
|
|
|
# sed -i "s/localhost/$MYSQL_HOST/" $MISP_APP_CONFIG_PATH/database.php
|
|
|
|
# sed -i "s/db\s*login/$MYSQL_USER/" $MISP_APP_CONFIG_PATH/database.php
|
2023-10-31 14:49:48 +01:00
|
|
|
# sed -i "s/3306/$MYSQL_PORT/" $MISP_APP_CONFIG_PATH/database.php
|
2023-10-16 11:20:00 +02:00
|
|
|
# sed -i "s/db\s*password/$MYSQL_PASSWORD/" $MISP_APP_CONFIG_PATH/database.php
|
|
|
|
# sed -i "s/'database' => 'misp'/'database' => '$MYSQL_DATABASE'/" $MISP_APP_CONFIG_PATH/database.php
|
|
|
|
chmod +w $MISP_APP_CONFIG_PATH/database.php
|
|
|
|
sed "s/localhost/$MYSQL_HOST/" $MISP_APP_CONFIG_PATH/database.php > tmp; cat tmp > $MISP_APP_CONFIG_PATH/database.php; rm tmp
|
|
|
|
sed "s/db\s*login/$MYSQL_USER/" $MISP_APP_CONFIG_PATH/database.php > tmp; cat tmp > $MISP_APP_CONFIG_PATH/database.php; rm tmp
|
2023-10-31 14:49:48 +01:00
|
|
|
sed "s/3306/$MYSQL_PORT/" $MISP_APP_CONFIG_PATH/database.php > tmp; cat tmp > $MISP_APP_CONFIG_PATH/database.php; rm tmp
|
2023-10-16 11:20:00 +02:00
|
|
|
sed "s/db\s*password/$MYSQL_PASSWORD/" $MISP_APP_CONFIG_PATH/database.php > tmp; cat tmp > $MISP_APP_CONFIG_PATH/database.php; rm tmp
|
|
|
|
sed "s/'database' => 'misp'/'database' => '$MYSQL_DATABASE'/" $MISP_APP_CONFIG_PATH/database.php > tmp; cat tmp > $MISP_APP_CONFIG_PATH/database.php; rm tmp
|
2023-04-13 16:02:02 +02:00
|
|
|
|
2023-05-15 13:13:25 +02:00
|
|
|
echo "... initialize email.php settings"
|
2023-05-14 17:56:55 +02:00
|
|
|
chmod +w $MISP_APP_CONFIG_PATH/email.php
|
|
|
|
tee $MISP_APP_CONFIG_PATH/email.php > /dev/null <<EOT
|
2023-04-13 16:02:02 +02:00
|
|
|
<?php
|
|
|
|
class EmailConfig {
|
|
|
|
public \$default = array(
|
|
|
|
'transport' => 'Smtp',
|
|
|
|
'from' => array('misp-dev@admin.test' => 'Misp DEV'),
|
|
|
|
'host' => 'mail',
|
|
|
|
'port' => 25,
|
|
|
|
'timeout' => 30,
|
|
|
|
'client' => null,
|
|
|
|
'log' => false,
|
|
|
|
);
|
|
|
|
public \$smtp = array(
|
|
|
|
'transport' => 'Smtp',
|
|
|
|
'from' => array('misp-dev@admin.test' => 'Misp DEV'),
|
|
|
|
'host' => 'mail',
|
|
|
|
'port' => 25,
|
|
|
|
'timeout' => 30,
|
|
|
|
'client' => null,
|
|
|
|
'log' => false,
|
|
|
|
);
|
|
|
|
public \$fast = array(
|
|
|
|
'from' => 'misp-dev@admin.test',
|
|
|
|
'sender' => null,
|
|
|
|
'to' => null,
|
|
|
|
'cc' => null,
|
|
|
|
'bcc' => null,
|
|
|
|
'replyTo' => null,
|
|
|
|
'readReceipt' => null,
|
|
|
|
'returnPath' => null,
|
|
|
|
'messageId' => true,
|
|
|
|
'subject' => null,
|
|
|
|
'message' => null,
|
|
|
|
'headers' => null,
|
|
|
|
'viewRender' => null,
|
|
|
|
'template' => false,
|
|
|
|
'layout' => false,
|
|
|
|
'viewVars' => null,
|
|
|
|
'attachments' => null,
|
|
|
|
'emailFormat' => null,
|
|
|
|
'transport' => 'Smtp',
|
|
|
|
'host' => 'mail',
|
|
|
|
'port' => 25,
|
|
|
|
'timeout' => 30,
|
|
|
|
'client' => null,
|
|
|
|
'log' => true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
EOT
|
2023-05-14 17:56:55 +02:00
|
|
|
chmod -w $MISP_APP_CONFIG_PATH/email.php
|
2023-04-13 16:02:02 +02:00
|
|
|
|
|
|
|
# Init files (shared with host)
|
2023-05-15 13:13:25 +02:00
|
|
|
echo "... initialize app files"
|
2023-04-13 16:02:02 +02:00
|
|
|
MISP_APP_FILES_PATH=/var/www/MISP/app/files
|
|
|
|
if [ ! -f ${MISP_APP_FILES_PATH}/INIT ]; then
|
|
|
|
cp -R ${MISP_APP_FILES_PATH}.dist/* ${MISP_APP_FILES_PATH}
|
|
|
|
touch ${MISP_APP_FILES_PATH}/INIT
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
update_misp_data_files(){
|
2020-05-29 01:49:27 +02:00
|
|
|
for DIR in $(ls /var/www/MISP/app/files.dist); do
|
2023-04-13 16:02:02 +02:00
|
|
|
echo "... rsync -azh --delete \"/var/www/MISP/app/files.dist/$DIR\" \"/var/www/MISP/app/files/\""
|
2020-05-29 01:51:41 +02:00
|
|
|
rsync -azh --delete "/var/www/MISP/app/files.dist/$DIR" "/var/www/MISP/app/files/"
|
2020-05-29 01:49:27 +02:00
|
|
|
done
|
2020-05-29 01:38:33 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 16:02:02 +02:00
|
|
|
enforce_misp_data_permissions(){
|
|
|
|
echo "... chown -R www-data:www-data /var/www/MISP/app/tmp" && find /var/www/MISP/app/tmp \( ! -user www-data -or ! -group www-data \) -exec chown www-data:www-data {} +
|
|
|
|
# Files are also executable and read only, because we have some rogue scripts like 'cake' and we can not do a full inventory
|
|
|
|
echo "... chmod -R 0550 files /var/www/MISP/app/tmp" && find /var/www/MISP/app/tmp -not -perm 550 -type f -exec chmod 0550 {} +
|
|
|
|
# Directories are also writable, because there seems to be a requirement to add new files every once in a while
|
|
|
|
echo "... chmod -R 0770 directories /var/www/MISP/app/tmp" && find /var/www/MISP/app/tmp -not -perm 770 -type d -exec chmod 0770 {} +
|
|
|
|
# We make 'files' and 'tmp' (logs) directories and files user and group writable (we removed the SGID bit)
|
|
|
|
echo "... chmod -R u+w,g+w /var/www/MISP/app/tmp" && chmod -R u+w,g+w /var/www/MISP/app/tmp
|
|
|
|
|
|
|
|
echo "... chown -R www-data:www-data /var/www/MISP/app/files" && find /var/www/MISP/app/files \( ! -user www-data -or ! -group www-data \) -exec chown www-data:www-data {} +
|
|
|
|
# Files are also executable and read only, because we have some rogue scripts like 'cake' and we can not do a full inventory
|
|
|
|
echo "... chmod -R 0550 files /var/www/MISP/app/files" && find /var/www/MISP/app/files -not -perm 550 -type f -exec chmod 0550 {} +
|
|
|
|
# Directories are also writable, because there seems to be a requirement to add new files every once in a while
|
|
|
|
echo "... chmod -R 0770 directories /var/www/MISP/app/files" && find /var/www/MISP/app/files -not -perm 770 -type d -exec chmod 0770 {} +
|
|
|
|
# We make 'files' and 'tmp' (logs) directories and files user and group writable (we removed the SGID bit)
|
|
|
|
echo "... chmod -R u+w,g+w /var/www/MISP/app/files" && chmod -R u+w,g+w /var/www/MISP/app/files
|
|
|
|
|
|
|
|
echo "... chown -R www-data:www-data /var/www/MISP/app/Config" && find /var/www/MISP/app/Config \( ! -user www-data -or ! -group www-data \) -exec chown www-data:www-data {} +
|
|
|
|
# Files are also executable and read only, because we have some rogue scripts like 'cake' and we can not do a full inventory
|
|
|
|
echo "... chmod -R 0550 files /var/www/MISP/app/Config ..." && find /var/www/MISP/app/Config -not -perm 550 -type f -exec chmod 0550 {} +
|
|
|
|
# Directories are also writable, because there seems to be a requirement to add new files every once in a while
|
|
|
|
echo "... chmod -R 0770 directories /var/www/MISP/app/Config" && find /var/www/MISP/app/Config -not -perm 770 -type d -exec chmod 0770 {} +
|
|
|
|
# We make configuration files read only
|
|
|
|
echo "... chmod 600 /var/www/MISP/app/Config/{config,database,email}.php" && chmod 600 /var/www/MISP/app/Config/{config,database,email}.php
|
|
|
|
}
|
|
|
|
|
|
|
|
flip_nginx() {
|
|
|
|
local live="$1";
|
|
|
|
local reload="$2";
|
|
|
|
|
|
|
|
if [[ "$live" = "true" ]]; then
|
|
|
|
NGINX_DOC_ROOT=/var/www/MISP/app/webroot
|
|
|
|
elif [[ -x /custom/files/var/www/html/index.php ]]; then
|
|
|
|
NGINX_DOC_ROOT=/custom/files/var/www/html/
|
|
|
|
else
|
|
|
|
NGINX_DOC_ROOT=/var/www/html/
|
2020-05-29 02:23:33 +02:00
|
|
|
fi
|
|
|
|
|
2023-04-13 16:02:02 +02:00
|
|
|
# must be valid for all roots
|
|
|
|
echo "... nginx docroot set to ${NGINX_DOC_ROOT}"
|
|
|
|
sed -i "s|root.*var/www.*|root ${NGINX_DOC_ROOT};|" /etc/nginx/sites-available/misp
|
2019-11-25 22:58:18 +01:00
|
|
|
|
2023-04-13 16:02:02 +02:00
|
|
|
if [[ "$reload" = "true" ]]; then
|
|
|
|
echo "... nginx reloaded"
|
|
|
|
nginx -s reload
|
|
|
|
fi
|
|
|
|
}
|
2020-02-21 16:14:43 +01:00
|
|
|
|
2023-04-13 16:02:02 +02:00
|
|
|
init_nginx() {
|
2023-08-01 12:21:12 +02:00
|
|
|
# Testing for files also test for links, and generalize better to mounted files
|
|
|
|
if [[ ! -f "/etc/nginx/sites-enabled/misp80" ]]; then
|
2023-04-13 16:02:02 +02:00
|
|
|
echo "... enabling port 80 redirect"
|
2023-09-25 22:40:13 +02:00
|
|
|
if [[ "$DISABLE_IPV6" = "true" ]]; then
|
|
|
|
sed -i "/\[::\]/d" /etc/nginx/sites-available/misp80
|
|
|
|
fi
|
2023-04-13 16:02:02 +02:00
|
|
|
ln -s /etc/nginx/sites-available/misp80 /etc/nginx/sites-enabled/misp80
|
|
|
|
else
|
|
|
|
echo "... port 80 already configured"
|
|
|
|
fi
|
2020-02-27 21:28:21 +01:00
|
|
|
|
2023-08-01 12:21:12 +02:00
|
|
|
# Testing for files also test for links, and generalize better to mounted files
|
|
|
|
if [[ ! -f "/etc/nginx/sites-enabled/misp" ]]; then
|
2023-04-13 16:02:02 +02:00
|
|
|
echo "... enabling port 443"
|
2023-09-25 22:40:13 +02:00
|
|
|
if [[ "$DISABLE_IPV6" = "true" ]]; then
|
|
|
|
sed -i "/\[::\]/d" /etc/nginx/sites-available/misp
|
|
|
|
fi
|
2023-04-13 16:02:02 +02:00
|
|
|
ln -s /etc/nginx/sites-available/misp /etc/nginx/sites-enabled/misp
|
|
|
|
else
|
|
|
|
echo "... port 443 already configured"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -f /etc/nginx/certs/cert.pem || ! -f /etc/nginx/certs/key.pem ]]; then
|
|
|
|
echo "... generating new self-signed TLS certificate"
|
|
|
|
openssl req -x509 -subj '/CN=localhost' -nodes -newkey rsa:4096 -keyout /etc/nginx/certs/key.pem -out /etc/nginx/certs/cert.pem -days 365
|
|
|
|
else
|
|
|
|
echo "... TLS certificates found"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -f /etc/nginx/certs/dhparams.pem ]]; then
|
|
|
|
echo "... generating new DH parameters"
|
|
|
|
openssl dhparam -out /etc/nginx/certs/dhparams.pem 2048
|
|
|
|
else
|
|
|
|
echo "... DH parameters found"
|
|
|
|
fi
|
2020-05-29 01:33:54 +02:00
|
|
|
|
2023-04-13 16:02:02 +02:00
|
|
|
flip_nginx false false
|
|
|
|
}
|
2020-05-29 01:33:54 +02:00
|
|
|
|
2021-04-07 16:42:20 +02:00
|
|
|
|
2023-04-13 16:02:02 +02:00
|
|
|
# Initialize MySQL
|
|
|
|
echo "INIT | Initialize MySQL ..." && init_mysql
|
2021-04-07 16:42:20 +02:00
|
|
|
|
2023-04-13 16:02:02 +02:00
|
|
|
# Initialize NGINX
|
|
|
|
echo "INIT | Initialize NGINX ..." && init_nginx
|
|
|
|
nginx -g 'daemon off;' & master_pid=$!
|
2020-03-12 14:40:27 +01:00
|
|
|
|
2023-04-13 16:02:02 +02:00
|
|
|
# Initialize MISP
|
|
|
|
echo "INIT | Initialize MISP files and configurations ..." && init_misp_data_files
|
2023-05-15 13:13:25 +02:00
|
|
|
echo "INIT | Update MISP app/files directory ..." && update_misp_data_files
|
2023-04-13 16:02:02 +02:00
|
|
|
echo "INIT | Enforce MISP permissions ..." && enforce_misp_data_permissions
|
2023-05-15 13:13:25 +02:00
|
|
|
echo "INIT | Flip NGINX live ..." && flip_nginx true true
|
2020-03-06 17:00:35 +01:00
|
|
|
|
2023-04-13 16:02:02 +02:00
|
|
|
# Run configure MISP script
|
2023-05-15 13:13:25 +02:00
|
|
|
echo "INIT | Configure MISP installation ..."
|
2023-04-13 16:02:02 +02:00
|
|
|
/configure_misp.sh
|
2020-05-29 02:23:33 +02:00
|
|
|
|
2023-04-13 16:02:02 +02:00
|
|
|
if [[ -x /custom/files/customize_misp.sh ]]; then
|
2023-05-15 13:13:25 +02:00
|
|
|
echo "INIT | Customize MISP installation ..."
|
2023-04-13 16:02:02 +02:00
|
|
|
/custom/files/customize_misp.sh
|
2022-11-04 18:22:12 +01:00
|
|
|
fi
|
|
|
|
|
2022-11-30 17:42:16 +01:00
|
|
|
# Wait for it
|
|
|
|
wait "$master_pid"
|