Skip to content

Commit

Permalink
Changes for #1128
Browse files Browse the repository at this point in the history
  • Loading branch information
the-djmaze committed May 16, 2023
1 parent a504fd9 commit 35bfff8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
32 changes: 32 additions & 0 deletions integrations/hestiacp/hestia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

$_ENV['SNAPPYMAIL_INCLUDE_AS_API'] = true;
require_once '/var/lib/snappymail/index.php';

\SnappyMail\Repository::installPackage('plugin', 'change-password');
\SnappyMail\Repository::installPackage('plugin', 'change-password-hestia');
$sFile = APP_PRIVATE_DATA.'configs/plugin-change-password.json';
if (!file_exists($sFile)) {
file_put_contents('', json_encode([
'plugin' => [
'pass_min_length': 8,
'pass_min_strength': 60,
'driver_hestia_enabled': true,
'driver_hestia_allowed_emails': '*',
'hestia_host': gethostname(),
'hestia_port': 8083 // $BACKEND_PORT
]
], JSON_PRETTY_PRINT));
}
// \SnappyMail\Repository::enablePackage('change-password');

\SnappyMail\Repository::installPackage('plugin', 'add-x-originating-ip-header');
// \SnappyMail\Repository::enablePackage('add-x-originating-ip-header');

$sFile = APP_PRIVATE_DATA.'domains/hestia.json';
if (!file_exists($sFile)) {
$config = json_decode(file_get_contents(__DIR__ . '/app/domains/default.json'), true);
$config['IMAP']['shortLogin'] = true;
$config['SMTP']['shortLogin'] = true;
file_put_contents($sFile, json_encode($config, JSON_PRETTY_PRINT));
}
40 changes: 12 additions & 28 deletions integrations/hestiacp/v-add-sys-snappymail
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ fi
# Get current version
if [ -f "/var/lib/snappymail/data/VERSION" ]; then
version=$(cat $SM_INSTALL_DIR/data/VERSION);
if [ "$version" == "$rl_v" ]; then
echo "Error: Installed version ($version) is equal to the available version ($rl_v)"
if [ "$version" == "$sm_v" ]; then
echo "Error: Installed version ($version) is equal to the available version ($sm_v)"
exit 2;
else
UPDATE="yes"
Expand All @@ -80,13 +80,19 @@ if [ "$UPDATE" == "no" ]; then
mkdir $SM_CONFIG_DIR

cd "$SM_INSTALL_DIR"
[ ! -f "${RC_INSTALL_DIR}/${RC_FILE}" ] && wget "$SM_URL" --retry-connrefused --quiet -O "${SM_INSTALL_DIR}/${SM_FILE}"
[ ! -f "${SM_INSTALL_DIR}/${SM_FILE}" ] && wget "$SM_URL" --retry-connrefused --quiet -O "${SM_INSTALL_DIR}/${SM_FILE}"

if [ -f "${SM_INSTALL_DIR}/${SM_FILE}" ]; then
echo "ERROR: Download failed, installation aborted."
exit 2
fi

# Get current version

key=$(openssl rand -hex 4);

admin_account="admin_$key"
admin_password=$(generate_password)
r=$(generate_password)

echo "Username: admin_$key" > ~/.snappymail
echo "Password: $admin_password" >> ~/.snappymail
Expand All @@ -97,36 +103,14 @@ if [ "$UPDATE" == "no" ]; then
mv ./data $SM_CONFIG_DIR/
ln -s $SM_CONFIG_DIR/data/ ./data

SALT=$(openssl rand -base64 64)
cp ./data/VERSION ./data/INSTALLED
echo "<?php //$SALT" > ./data/SALT.php
echo "Forbidden" > ./data/index.php
echo "Forbidden" > ./data/index.html

# Create database
mysql -e "CREATE DATABASE snappymail"
# Mysql available on system
r=$(generate_password)
mysql -e "GRANT ALL ON snappymail.*
TO snappymail@localhost IDENTIFIED BY '$r'"

mkdir -p $SM_CONFIG_DIR/data/_data_/_default_/configs
php -f $HESTIA_INSTALL_DIR/snappymail/change_password.php "admin_$key" "$admin_password" "$r"
mkdir -p $SM_CONFIG_DIR/data/_data_/_default_/domains
cp -f $HESTIA_INSTALL_DIR/snappymail/default.ini $SM_CONFIG_DIR/data/_data_/_default_/domains
mkdir -p $SM_CONFIG_DIR/data/_data_/_default_/plugins
cp -f -r $HESTIA_INSTALL_DIR/snappymail/plugins/hestia-change-password/ $SM_CONFIG_DIR/data/_data_/_default_/plugins
mkdir -p $SM_CONFIG_DIR/data/_data_/_default_/plugins/add-x-originating-ip-header
# Download add-x-originating-ip-header from snappymail github
wget --quiet -O $SM_CONFIG_DIR/data/_data_/_default_/plugins/add-x-originating-ip-header/index.php https://raw.githubusercontent.com/the-djmaze/snappymail/master/plugins/add-x-originating-ip-header/index.php
wget --quiet -O $SM_CONFIG_DIR/data/_data_/_default_/plugins/add-x-originating-ip-header/LICENSE https://raw.githubusercontent.com/the-djmaze/snappymail/master/plugins/add-x-originating-ip-header/LICENSE

cp -f $HESTIA_INSTALL_DIR/snappymail/plugins/plugin-add-x-originating-ip-header.ini $SM_CONFIG_DIR/data/_data_/_default_/configs/plugin-add-x-originating-ip-header.ini
cp -f $HESTIA_INSTALL_DIR/snappymail/plugins/plugin-hestia-change-password.ini $SM_CONFIG_DIR/data/_data_/_default_/configs/plugin-hestia-change-password.ini

sed -i "s/%hostname%/$(hostname)/g" $SM_CONFIG_DIR/data/_data_/_default_/configs/plugin-hestia-change-password.ini
sed -i "s/%port%/$BACKEND_PORT/g" $SM_CONFIG_DIR/data/_data_/_default_/configs/plugin-hestia-change-password.ini

php -f $HESTIA_INSTALL_DIR/snappymail/hestia.php "admin_$key" "$admin_password" "$r"

chown -R www-data:www-data ./data
chown -R www-data:www-data $SM_CONFIG_DIR/
Expand All @@ -150,7 +134,7 @@ if [ "$UPDATE" == "no" ]; then
fi

else
[ ! -f "${RC_INSTALL_DIR}/${RC_FILE}" ] && wget "$SM_URL" --quiet -O "${SM_INSTALL_DIR}/${SM_FILE}"
[ ! -f "${SM_INSTALL_DIR}/${SM_FILE}" ] && wget "$SM_URL" --quiet -O "${SM_INSTALL_DIR}/${SM_FILE}"
version=$(cat $SM_INSTALL_DIR/data/VERSION);

tar -xzf snappymail-latest.tar.gz "data/VERSION" $SM_INSTALL_DIR/
Expand Down
38 changes: 15 additions & 23 deletions plugins/change-password-hestia/driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,26 @@ public static function configMapping() : array
->SetDescription('Ex: localhost or domain.com'),
\RainLoop\Plugins\Property::NewInstance('hestia_port')->SetLabel('Hestia Port')
->SetType(\RainLoop\Enumerations\PluginPropertyType::INT)
->SetDefaultValue(8083),
\RainLoop\Plugins\Property::NewInstance('hestia_allowed_emails')->SetLabel('Allowed emails')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT)
->SetDescription('Allowed emails, space as delimiter, wildcard supported. Example: [email protected] [email protected] *@domain2.net')
->SetDefaultValue('*')
->SetDefaultValue(8083)
);
}

public function ChangePassword(\RainLoop\Model\Account $oAccount, string $sPrevPassword, string $sNewPassword) : bool
{
if (!\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->oConfig->Get('plugin', 'hestia_allowed_emails', ''))) {
return false;
}

$sHost = $this->oConfig->Get('plugin', 'hestia_login');
$sPort = $this->oConfig->Get('plugin', 'hestia_port');

$HTTP = \SnappyMail\HTTP\Request::factory();
$postvars = array(
'email' => $oAccount->Email(),
'password' => $sPrevPassword,
'new' => $sNewPassword,
);
$cRequest = $HTTP->doRequest('POST','https://'.$sHost.':'.$sPort.'/reset/mail/',http_build_query($postvars));
if($cRequest -> body == '==ok=='){
return true;
}else{
return false;
}
return false;
}

$sHost = $this->oConfig->Get('plugin', 'hestia_login');
$sPort = $this->oConfig->Get('plugin', 'hestia_port');

$HTTP = \SnappyMail\HTTP\Request::factory();
$postvars = array(
'email' => $oAccount->Email(),
'password' => $sPrevPassword,
'new' => $sNewPassword,
);
$cRequest = $HTTP->doRequest('POST','https://'.$sHost.':'.$sPort.'/reset/mail/',http_build_query($postvars));
return '==ok==' == $cRequest->body;
}
}

0 comments on commit 35bfff8

Please sign in to comment.