Skip to content

Commit

Permalink
Update GeoIP to support authentication by account id+license key
Browse files Browse the repository at this point in the history
Fix #1301
Replace #1302
  • Loading branch information
endelwar committed Aug 10, 2024
1 parent 4b58985 commit 4bbc9a4
Show file tree
Hide file tree
Showing 69 changed files with 4,476 additions and 3,667 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ jobs:
uses: "ramsey/composer-install@v2"

- name: Run PHPStan
run: phpstan analyse --configuration=phpstan.neon.dist --error-format=github
run: phpstan analyse --configuration=phpstan.dist.neon --error-format=github
11 changes: 8 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
$finder = (new PhpCsFixer\Finder())
->in('mailscanner')->exclude('lib')
->in('tools')

;

return (new PhpCsFixer\Config())
->setFinder($finder)
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
Expand All @@ -16,7 +16,12 @@
'concat_space' => ['spacing' => 'one'],
'cast_spaces' => ['space' => 'none'],
'native_function_invocation' => false,
'no_superfluous_phpdoc_tags' => true,
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true, 'remove_inheritdoc' => true],
'fopen_flags' => ['b_mode' => true],
'function_declaration' => ['closure_function_spacing' => 'none', 'closure_fn_spacing' => 'none'],
'phpdoc_summary' => false,
'phpdoc_no_package' => false,
'phpdoc_separation' => ['groups' => [['ORM\\*'], ['Assert\\*'], ['Serializer\\*']]],
'fully_qualified_strict_types' => false,
])
->setFinder($finder)
;
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"php-parallel-lint/php-parallel-lint": "^1.4",
"php-parallel-lint/php-console-highlighter": "^1.0.0",
"rector/rector": "^1.2",
"phpunit/phpunit": "^9.6"
"phpunit/phpunit": "^9.6",
"symfony/var-dumper": "^7.1"
},
"autoload-dev": {
"psr-4": {
Expand Down
107 changes: 95 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions mailscanner/conf.php.example
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ define('LANG', 'en');
// in the gui to change the language of his browser. The selectable languages are defined as a comma separated list.
define('USER_SELECTABLE_LANG', 'de,en,es-419,fr,it,ja,nl,pt_br');

// Session Handling - conflicts can exist when the your environment makes use of multiple php sessions on the same server
// to resolve this, uncomment the following option. See https://github.com/mailwatch/MailWatch/issues/730 for more info
// A valid session name may consists of digits, letters A to Z (both upper and lower case), comma and dash
// Session Handling - conflicts can exist when your environment makes use of multiple php sessions on the same server
// to resolve this, uncomment the following option. See https://github.com/mailwatch/MailWatch/issues/730 for more info
// A valid session name may consist of digits, letters A to Z (both upper and lower case), comma and dash
//define('SESSION_NAME', 'MailWatch');

// Session Timeout - Sets the global session timeout value, default is 600 sec (10 minutes) if not defined
Expand All @@ -53,6 +53,7 @@ define('SESSION_TIMEOUT', 600);
// A free license key from MaxMind is required to download GeoLite2 data
// https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-geolite2-databases/
// define('MAXMIND_LICENSE_KEY', 'mylicensekey');
// define('MAXMIND_ACCOUNT_ID', 'myaccountid');

// Database settings
//
Expand Down
1 change: 1 addition & 0 deletions mailscanner/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4297,6 +4297,7 @@ function checkConfVariables()
'IMAP_HOST' => ['description' => 'IMAP host to be used for user authentication'],
'IMAP_AUTOCREATE_VALID_USER' => ['description' => 'enable to autorcreate user from valid imap login'],
'MAXMIND_LICENSE_KEY' => ['description' => 'needed to download MaxMind GeoLite2 data'],
'MAXMIND_ACCOUNT_ID' => ['description' => 'needed to download MaxMind GeoLite2 data'],
'QUARANTINE_DAYS_TO_KEEP_NONSPAM' => ['description' => 'to have quarantine keeping days independently configured for nonspam mails'],
];

Expand Down
44 changes: 38 additions & 6 deletions mailscanner/geoip_update.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

/*
/**
* MailWatch for MailScanner
* Copyright (C) 2003-2011 Steve Freegard ([email protected])
* Copyright (C) 2011 Garrod Alwood ([email protected])
Expand Down Expand Up @@ -46,7 +46,7 @@
<tr>
<td>
<br>
' . __('message115') . ' <a href="https://dev.maxmind.com/geoip/geoip2/geolite2/" target="_maxmind">MaxMind</a> ' . __('message215') . '<br><br>
' . __('message115') . ' <a href="https://dev.maxmind.com/geoip/geolite2-free-geolocation-data" target="_maxmind">MaxMind</a> ' . __('message215') . '<br><br>
</td>
</tr>
<tr>
Expand All @@ -63,7 +63,8 @@

$files_base_url = 'https://download.maxmind.com';
$file['description'] = __('geoip15');
$file['path'] = '/app/geoip_download?edition_id=GeoLite2-Country&suffix=tar.gz&license_key=' . MAXMIND_LICENSE_KEY;
$file['path'] = '/geoip/databases/GeoLite2-Country/download?suffix=tar.gz';
$file['legacy_path'] = '/app/geoip_download?edition_id=GeoLite2-Country&suffix=tar.gz&license_key=' . MAXMIND_LICENSE_KEY;
$file['destination'] = __DIR__ . '/temp/GeoLite2-Country.tar.gz';
$file['destinationFileName'] = 'GeoLite2-Country.mmdb';

Expand Down Expand Up @@ -112,12 +113,36 @@

try {
$requestSession->options['filename'] = $file['destination'];
$result = $requestSession->get($file['path']);
if (defined('MAXMIND_ACCOUNT_ID')) {
// use basic auth
$requestSession->options['auth'] = new Requests_Auth_Basic([MAXMIND_ACCOUNT_ID, MAXMIND_LICENSE_KEY]);

// remove auth and hooks from redirect request
$hooks = new Requests_Hooks();
$hooks->register('requests.before_redirect', function (
&$location,
&$req_headers,
&$req_data,
&$options
) {
$options['auth'] = false;
$options['hooks'] = new Requests_Hooks();
});
$requestSession->options['hooks'] = $hooks;
$result = $requestSession->get($file['path']);
} else {
$result = $requestSession->get($file['legacy_path']);
}
$result->throw_for_status();
if (true === $result->success) {
echo $file['description'] . ' ' . __('downok15') . '<br>' . "\n";
}
} catch (Requests_Exception $e) {
echo __('downbad15') . ' ' . $file['description'] . __('colon99') . ' ' . $e->getMessage() . "<br>\n";
echo sprintf('%s %s%s %s', __('downbad15'), $file['description'], __('colon99'), $e->getMessage());
if (file_exists($file['destination'])) {
echo sprintf(' (%s)', strip_tags(file_get_contents($file['destination'])));
}
exit;
}

ob_flush();
Expand All @@ -137,7 +162,13 @@
}
}

$command = escapeshellcmd('wget ' . $proxyString . ' -N ' . $files_base_url . $file['path'] . ' -O ' . $file['destination']);
if (defined('MAXMIND_ACCOUNT_ID')) {
$wget_basic_auth = sprintf('--user=%s --password=%s', MAXMIND_ACCOUNT_ID, MAXMIND_LICENSE_KEY);
$command = escapeshellcmd('wget ' . $wget_basic_auth . $proxyString . ' -N ' . $files_base_url . $file['path'] . ' -O ' . $file['destination']);
} else {
$command = escapeshellcmd('wget ' . $proxyString . ' -N ' . $files_base_url . $file['path'] . ' -O ' . $file['destination']);
}

$result = exec(
$command,
$output_wget,
Expand All @@ -146,6 +177,7 @@

if ($retval_wget > 0) {
echo __('downbad15') . ' ' . $file['description'] . "<br>\n";
exit;
} else {
echo $file['description'] . ' ' . __('downok15') . '<br>' . "\n";
}
Expand Down
Loading

0 comments on commit 4bbc9a4

Please sign in to comment.