Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GeoIP to support authentication by account id+license key #1302

Open
wants to merge 2 commits into
base: 1.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mailscanner/conf.php.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
56 changes: 28 additions & 28 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 (steve@freegard.name)
* Copyright (C) 2011 Garrod Alwood (garrod.alwood@lorodoes.com)
Expand Down Expand Up @@ -33,7 +33,7 @@

html_start(__('geoipupdate15'), 0, false, false);

if (!defined('MAXMIND_LICENSE_KEY') || !validateInput(MAXMIND_LICENSE_KEY, 'maxmind')) {
if (!defined('MAXMIND_LICENSE_KEY') || !defined('MAXMIND_ACCOUNT_ID') || !validateInput(MAXMIND_LICENSE_KEY, 'maxmind')) {
$error_message = __('geoipnokey15') . '<br>' . "\n";
exit($error_message);
} elseif (!isset($_POST['run'])) {
Expand All @@ -55,15 +55,13 @@
</table>
</form>' . "\n";
} else {
require_once __DIR__ . '/lib/request/Requests.php';
Requests::register_autoloader();

ob_start();
echo __('downfile15') . '<br>' . "\n";

$files_base_url = 'https://download.maxmind.com';
$urlSchema = 'https://';
$downloadServer = 'download.maxmind.com';
$file['description'] = __('geoip15');
$file['path'] = '/app/geoip_download?edition_id=GeoLite2-Country&suffix=tar.gz&license_key=' . MAXMIND_LICENSE_KEY;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still working for my legacy Maxmind key, should we fall back and attempt this download when MAXMIND_ACCOUNT_ID is not populated?

$file['path'] = '/geoip/databases/GeoLite2-Country/download?suffix=tar.gz';
$file['destination'] = __DIR__ . '/temp/GeoLite2-Country.tar.gz';
$file['destinationFileName'] = 'GeoLite2-Country.mmdb';

Expand All @@ -80,44 +78,46 @@
if (!file_exists($file['destination'])) {
if (is_writable($extract_dir) && is_readable($extract_dir)) {
if (function_exists('fsockopen') || extension_loaded('curl')) {
$requestSession = new Requests_Session($files_base_url . '/');
$requestSession->options['useragent'] = 'MailWatch/' . mailwatch_version();
if (USE_PROXY === true) {
$ch = curl_init($urlSchema . $downloadServer . $file['path']);

Check notice on line 81 in mailscanner/geoip_update.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

mailscanner/geoip_update.php#L81

Concat operator must not be surrounded by spaces

Check warning on line 81 in mailscanner/geoip_update.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

mailscanner/geoip_update.php#L81

The use of function curl_init() is discouraged
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

Check warning on line 82 in mailscanner/geoip_update.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

mailscanner/geoip_update.php#L82

The use of function curl_setopt() is discouraged
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERNAME, MAXMIND_ACCOUNT_ID);
curl_setopt($ch, CURLOPT_PASSWORD, MAXMIND_LICENSE_KEY);
curl_setopt($ch, CURLOPT_USERAGENT, 'MailWatch/' . mailwatch_version());
if (defined('USE_PROXY') && USE_PROXY === true) {
curl_setopt($ch, CURLOPT_PROXY, PROXY_SERVER);
curl_setopt($ch, CURLOPT_PROXYPORT, PROXY_PORT);
Comment on lines +81 to +89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try Requests_Auth_Basic in $requestSession->options['auth']? I don't like very much removing Requests integration

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I

Did you try Requests_Auth_Basic in $requestSession->options['auth']? I don't like very much removing Requests integration

To be honest I thought it might be good to remove the Request library as it is big piece of code for just downloading a file and it was orignally introduced for geoip download (if the commit history is correct) and isn't used anywhere else right now. But I'm fine if you want to keep it.

I did try the auth option of the requestsession but it didn't work that time. But I also didn't invest a lot of time trying. So it might just have been me 🤷.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, Requests was introduced simplify the geoip file download in 2015, and it proved useful to manage automatically the redirection that MaxMind apply on the download link when they switched to CloudFlare R2 storage: with curl you have to check the first response for a 301/302 redirect location and then make a second request to download from the correct location. The actual library in mailscanner/lib/request has also been patched to support HTTP/2 calls.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CURLOPT_FOLLOWLOCATION options tells curl to follow such redirects and I'm pretty sure that HTTP/2 is supported out of the box. Corresponding feature flags are supported since PHP 7.0.7 (release 05/2016) and matching curl versions also since 2016.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it seems anachronistic, but the 1.2.x branch needs to support PHP 5, so PHP 7+ solutions are a no go in 1.2 :(

We should start a 1.3 branch with php 7.4 support or even better align with officially supported only PHP version

if (PROXY_USER !== '') {
$requestSession->options['proxy']['authentication'] = [
PROXY_SERVER . ':' . PROXY_PORT,
PROXY_USER,
PROXY_PASS,
];
} else {
$requestSession->options['proxy']['authentication'] = [
PROXY_SERVER . ':' . PROXY_PORT,
];
curl_setopt($ch, CURLOPT_PROXYUSERPWD, PROXY_USER . ':' . PROXY_PASS);
}

switch (PROXY_TYPE) {
case 'HTTP':
case 'CURLPROXY_HTTP': // BC for old constant name
// $requestProxy = new Requests_Proxy_HTTP($requestProxyParams);
$requestSession->options['proxy']['type'] = 'HTTP';
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
break;
case 'SOCKS5':
case 'CURLPROXY_SOCKS5': // BC for old constant name
$requestSession->options['proxy']['type'] = 'SOCKS5';
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
break;
default:
exit(__('dieproxy15'));
}
}

try {
$requestSession->options['filename'] = $file['destination'];
$result = $requestSession->get($file['path']);
if (true === $result->success) {
$fpDestinationFile = fopen($file['destination'], 'w');
curl_setopt($ch, CURLOPT_FILE, $fpDestinationFile);
curl_exec($ch);
if (empty(curl_error($ch))) {
echo $file['description'] . ' ' . __('downok15') . '<br>' . "\n";
} else {
echo __('downbad15') . ' ' . $file['description'] . __('colon99') . ' ' . curl_error($ch) . "<br>\n";
}
} catch (Requests_Exception $e) {
echo __('downbad15') . ' ' . $file['description'] . __('colon99') . ' ' . $e->getMessage() . "<br>\n";
} catch (Exception $e) {
echo __('downbad15') . ' ' . $file['description'] . __('colon99') . ' ' . curl_error($ch) . "<br>\n";
} finally {
fclose($fpDestinationFile);
}

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

$command = escapeshellcmd('wget ' . $proxyString . ' -N ' . $files_base_url . $file['path'] . ' -O ' . $file['destination']);
$command = escapeshellcmd('wget ' . $proxyString . ' -N ' . $urlSchema . MAXMIND_ACCOUNT_ID . ':' . MAXMIND_LICENSE_KEY . '@' . $downloadServer . $file['path'] . ' -O ' . $file['destination']);
$result = exec(
$command,
$output_wget,
Expand Down
Loading