def set_hostname(hostname): """Sets machine hostname to hostname""" # Hostname should be ASCII. If it's unicode but passed our # valid_hostname check, convert to ASCII. hostname = str(hostname) LOGGER.info('Changing hostname to - %s', hostname) try: actions.superuser_run('xmpp-pre-hostname-change') actions.superuser_run('hostname-change', hostname) actions.superuser_run('xmpp-hostname-change', hostname, async=True) except OSError as err: raise ActionError(err.message)
def set_hostname(hostname): "Sets machine hostname to hostname" # Hostname should be ASCII. If it's unicode but passed our valid_hostname check, convert to ASCII. hostname = str(hostname) cfg.log.info("Changing hostname to '%s'" % hostname) try: superuser_run("hostname-change", hostname) # don't persist/cache change unless it was saved successfuly sys_store = filedict_con(cfg.store_file, 'sys') sys_store['hostname'] = hostname except OSError, e: raise cherrypy.HTTPError(500, "Updating hostname failed: %s" % e)
def test_run_as_root(self): """1. Privileged actions run as root. """ self.assertEqual( "0", # user 0 is root superuser_run("id", "-ur")[0].strip())
def _apply_changes(request, old_status, new_status): """Apply the form changes""" LOGGER.info('Status - %s, %s', old_status, new_status) if old_status['inband_enabled'] == new_status['inband_enabled']: messages.info(request, _('Setting unchanged')) return if new_status['inband_enabled']: messages.success(request, _('Inband registration enabled')) option = 'inband_enable' else: messages.success(request, _('Inband registration disabled')) option = 'noinband_enable' LOGGER.info('Option - %s', option) actions.superuser_run('xmpp-setup', [option])
def _apply_changes(request, old_status, new_status): """Apply the changes""" if old_status['enabled'] == new_status['enabled']: messages.info(request, _('Setting unchanged')) return if new_status['enabled']: messages.success(request, _('ownCloud enabled')) option = 'enable' else: messages.success(request, _('ownCloud disabled')) option = 'noenable' actions.superuser_run('owncloud-setup', [option], async=True) # Send a signal to other modules that the service is # enabled/disabled SERVICE.notify_enabled(None, new_status['enabled'])
def _run(arguments, superuser=True): """Run an given command and raise exception if there was an error""" command = 'pagekite-configure' LOGGER.info('Running command - %s, %s, %s', command, arguments, superuser) if superuser: output = actions.superuser_run(command, arguments) else: output = actions.run(command, arguments) return output
def test(request): """Run diagnostics and the output page""" output = "" error = "" try: output = actions.superuser_run("diagnostic-test") except ActionError as err: error = err.message return TemplateResponse(request, 'diagnostics_test.html', {'title': _('Diagnostic Test'), 'diagnostics_output': output, 'diagnostics_error': error})
def _register_user(request, data): """Register a new XMPP user""" output = actions.superuser_run( 'xmpp-register', [data['username'], data['password']]) if 'successfully registered' in output: messages.success(request, _('Registered account for %s') % data['username']) else: messages.error(request, _('Failed to register account for %s: %s') % (data['username'], output))
def _apply_changes(request, old_status, new_status): """Apply form changes""" for field, enabled in new_status.items(): if not field.endswith('_enabled'): continue if old_status[field] == new_status[field]: continue module = field.split('_enabled')[0] if enabled: try: actions.superuser_run('module-manager', ['enable', cfg.python_root, module]) except ActionError: # TODO: need to get plinth to load the module we just # enabled messages.error( request, _('Error enabling module - {module}').format( module=module)) else: messages.success( request, _('Module enabled - {module}').format(module=module)) else: try: actions.superuser_run('module-manager', ['disable', cfg.python_root, module]) except ActionError: # TODO: need a smoother way for plinth to unload the # module messages.error( request, _('Error disabling module - {module}').format( module=module)) else: messages.success( request, _('Module disabled - {module}').format(module=module))
def _register_user(request, data): """Register a new XMPP user""" output = actions.superuser_run('xmpp-register', [data['username'], data['password']]) if 'successfully registered' in output: messages.success(request, _('Registered account for %s') % data['username']) else: messages.error( request, _('Failed to register account for %s: %s') % (data['username'], output))
def _apply_changes(request, old_status, new_status): """Apply form changes""" for field, enabled in new_status.items(): if not field.endswith('_enabled'): continue if old_status[field] == new_status[field]: continue module = field.split('_enabled')[0] if enabled: try: actions.superuser_run( 'module-manager', ['enable', cfg.python_root, module]) except ActionError: # TODO: need to get plinth to load the module we just # enabled messages.error( request, _('Error enabling module - {module}').format( module=module)) else: messages.success( request, _('Module enabled - {module}').format( module=module)) else: try: actions.superuser_run( 'module-manager', ['disable', cfg.python_root, module]) except ActionError: # TODO: need a smoother way for plinth to unload the # module messages.error( request, _('Error disabling module - {module}').format( module=module)) else: messages.success( request, _('Module disabled - {module}').format( module=module))
def _apply_changes(request, old_status, new_status): """Apply the form changes""" if old_status['hostname'] != new_status['hostname']: try: set_hostname(new_status['hostname']) except (ActionError, ValueError) as err: messages.error(request, _('Error setting hostname: %s') % err.message) else: messages.success(request, _('Hostname set')) else: messages.info(request, _('Hostname is unchanged')) if old_status['time_zone'] != new_status['time_zone']: try: actions.superuser_run('timezone-change', [new_status['time_zone']]) except (ActionError, ValueError) as err: messages.error(request, _('Error setting time zone: %s') % err.message) else: messages.success(request, _('Time zone set')) else: messages.info(request, _('Time zone is unchanged'))
def test(request): """Run diagnostics and the output page""" output = "" error = "" try: output = actions.superuser_run("diagnostic-test") except ActionError as err: error = err.message return TemplateResponse( request, 'diagnostics_test.html', { 'title': _('Diagnostic Test'), 'diagnostics_output': output, 'diagnostics_error': error })
def index(request): """Service the index page""" output = actions.superuser_run("tor-get-ports") port_info = output.split("\n") tor_ports = {} for line in port_info: try: (key, val) = line.split() tor_ports[key] = val except ValueError: continue return TemplateResponse(request, 'tor.html', {'title': _('Tor Control Panel'), 'tor_ports': tor_ports})
def main(self, xmpp_inband_enable=False, message=None, *args, **kwargs): output, error = actions.superuser_run("xmpp-setup", 'status') if error: raise Exception("something is wrong: " + error) if "inband_enable" in output.split(): xmpp_inband_enable = True form = Form(title="Configure XMPP Server", action=cfg.server_dir + "/services/xmpp/configure/index", name="configure_xmpp_form", message=message) form.checkbox(_("Allow In-Band Registration"), name="xmpp_inband_enable", id="xmpp_inband_enable", checked=xmpp_inband_enable) # hidden field is needed because checkbox doesn't post if not checked form.hidden(name="submitted", value="True") form.html(_("<p>When enabled, anyone who can reach this server will be allowed to register an account through an XMPP client.</p>")) form.submit(_("Update setup")) return form.render()
def process_form(self, username=None, password=None, **kwargs): msg = Message() if not username: msg.add = _("Must specify a username!") if not password: msg.add = _("Must specify a password!") if username and password: output, error = actions.superuser_run( "xmpp-register", [username, password]) if error: raise Exception("something is wrong: " + error) if "successfully registered" in output: msg.add = _("Registered account for %s." % username) else: msg.add = _("Failed to register account for %s: %s" % (username, output)) cfg.log(msg.text) main = self.main(username, msg=msg.text) return self.fill_template( title="XMPP Server Configuration", main=main, sidebar_left=self.sidebar_left, sidebar_right=self.sidebar_right)
def notest_run_as_root(self): """1. Privileged actions run as root. """ # TODO: it's not allowed to call a symlink in the actions dir anymore self.assertEqual( "0", # user 0 is root superuser_run("id", "-ur")[0].strip())