diff --git a/actions/privoxy b/actions/privoxy index f07e4d72c..89b3fa29c 100755 --- a/actions/privoxy +++ b/actions/privoxy @@ -5,9 +5,14 @@ Configuration helper for Privoxy server. """ import argparse +import pathlib + +import augeas from plinth import action_utils +PRIVOXY_CONF_PATH = pathlib.Path('/etc/privoxy/config') + def parse_arguments(): """Return parsed command line arguments as dictionary.""" @@ -17,6 +22,7 @@ def parse_arguments(): subparsers.add_parser( 'pre-install', help='Preseed debconf values before packages are installed') + subparsers.add_parser('setup', help='Perform post install steps') subparsers.required = True return parser.parse_args() @@ -28,6 +34,49 @@ def subcommand_pre_install(_): ['privoxy privoxy/listen-address string [::]:8118']) +def subcommand_setup(_): + """Setup Privoxy configuration after installing it.""" + _restrict_access() + + +def _load_augeus(): + """Initialize Augeas.""" + aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + + augeas.Augeas.NO_MODL_AUTOLOAD) + aug.transform('Spacevars', str(PRIVOXY_CONF_PATH)) + aug.set('/augeas/context', '/files' + str(PRIVOXY_CONF_PATH)) + aug.load() + return aug + + +def _restrict_access(): + """Make sure Privoxy isn't available over the Internet.""" + # https://en.wikipedia.org/wiki/localhost + # https://en.wikipedia.org/wiki/Private_network + # https://en.wikipedia.org/wiki/Link-local_address + # https://en.wikipedia.org/wiki/Unique_local_address + ip_ranges = [ + '127.0.0.0/8', # IPv4 loopback address + '10.0.0.0/8', # IPv4 private address + '172.16.0.0/12', # IPv4 private address + '192.168.0.0/16', # IPv4 private address + '169.254.0.0/16', # IPv4 auto-configuration + '[::1]', # IPv4 loopback address + '[fc00::]/7', # IPv6 unique local addresses + '[fe80::]/10', # IPv6 auto-configuration + ] + aug = _load_augeus() + for ip_range in ip_ranges: + matches = [ + match for match in aug.match('permit-access') + if aug.get(match) == ip_range + ] + if not any(matches): + aug.set('permit-access[last() + 1]', ip_range) + + aug.save() + + def main(): """Parse arguments and perform all duties.""" arguments = parse_arguments() diff --git a/plinth/modules/privoxy/__init__.py b/plinth/modules/privoxy/__init__.py index 77ac1a2b0..432544723 100644 --- a/plinth/modules/privoxy/__init__.py +++ b/plinth/modules/privoxy/__init__.py @@ -26,7 +26,8 @@ _description = [ 'obnoxious Internet junk. '), format_lazy( _('You can use Privoxy by modifying your browser proxy settings to ' - 'your {box_name} hostname (or IP address) with port 8118. ' + 'your {box_name} hostname (or IP address) with port 8118. Only ' + 'connections from local network IP addresses are permitted. ' 'While using Privoxy, you can see its configuration details and ' 'documentation at ' 'http://config.privoxy.org/ ' @@ -42,7 +43,7 @@ class PrivoxyApp(app_module.App): app_id = 'privoxy' - _version = 1 + _version = 2 def __init__(self): """Create components for the app.""" @@ -98,6 +99,7 @@ def setup(helper, old_version=None): """Install and configure the module.""" helper.call('pre', actions.superuser_run, 'privoxy', ['pre-install']) app.setup(old_version) + helper.call('post', actions.superuser_run, 'privoxy', ['setup']) helper.call('post', app.enable)