def check_polkit():
    """
    Check if we have a running polkit agent and tries to launch an agent if
    needed.
    Show an error message if there is no agent and we couldn't run one.

    :return: True if we have a polkit running (or if we started one), False
             otherwise
    :rtype: bool
    """
    if LinuxPolicyChecker.is_up():
        return True

    try:
        LinuxPolicyChecker.maybe_pkexec()
        return True
    except Exception:
        logger.error("No polkit agent running.")

        msg = QtGui.QMessageBox()
        msg.setWindowTitle(msg.tr("No polkit agent running"))
        msg.setText(
            msg.tr('There is no polkit agent running and it is needed to run '
                   'the Bitmask services.<br>Take a look at the '
                   '<a href="https://leap.se/en/docs/client/known-issues">'
                   'known issues</a> page'))
        msg.setIcon(QtGui.QMessageBox.Critical)
        msg.exec_()

        return False
def check_polkit():
    """
    Check if we have a running polkit agent and tries to launch an agent if
    needed.
    Show an error message if there is no agent and we couldn't run one.

    :return: True if we have a polkit running (or if we started one), False
             otherwise
    :rtype: bool
    """
    if LinuxPolicyChecker.is_up():
        return True

    try:
        LinuxPolicyChecker.maybe_pkexec()
        return True
    except Exception:
        logger.error("No polkit agent running.")

        msg = QtGui.QMessageBox()
        msg.setWindowTitle(msg.tr("No polkit agent running"))
        msg.setText(
            msg.tr('There is no polkit agent running and it is needed to run '
                   'the Bitmask services.<br>Take a look at the '
                   '<a href="https://leap.se/en/docs/client/known-issues">'
                   'known issues</a> page'))
        msg.setIcon(QtGui.QMessageBox.Critical)
        msg.exec_()

        return False
    def _can_start(self, domain):
        """
        Returns True if it has everything that is needed to run EIP,
        False otherwise

        :param domain: the domain for the provider to check
        :type domain: str
        """
        if IS_LINUX and not LinuxPolicyChecker.is_up():
            logger.error("No polkit agent running.")
            return False

        provider_config = ProviderConfig.get_provider_config(domain)
        if EIP_SERVICE not in provider_config.get_services():
            return False
        eip_config = eipconfig.EIPConfig()

        api_version = provider_config.get_api_version()
        eip_config.set_api_version(api_version)
        eip_loaded = eip_config.load(eipconfig.get_eipconfig_path(domain))

        launcher = get_vpn_launcher()
        ovpn_path = force_eval(launcher.OPENVPN_BIN_PATH)
        if not os.path.isfile(ovpn_path):
            logger.error("Cannot start OpenVPN, binary not found: %s" %
                         (ovpn_path,))
            return False

        # check for other problems
        if not eip_loaded or provider_config is None:
            logger.error("Cannot load provider and eip config, cannot "
                         "autostart")
            return False

        client_cert_path = eip_config.\
            get_client_cert_path(provider_config, about_to_download=True)

        if leap_certs.should_redownload(client_cert_path):
            logger.error("The client should redownload the certificate,"
                         " cannot autostart")
            return False

        if not os.path.isfile(client_cert_path):
            logger.error("Can't find the certificate, cannot autostart")
            return False

        return True
    def _can_start(self, domain):
        """
        Returns True if it has everything that is needed to run EIP,
        False otherwise

        :param domain: the domain for the provider to check
        :type domain: str
        """
        if not LinuxPolicyChecker.is_up():
            logger.error("No polkit agent running.")
            return False

        eip_config = eipconfig.EIPConfig()
        provider_config = ProviderConfig.get_provider_config(domain)

        api_version = provider_config.get_api_version()
        eip_config.set_api_version(api_version)
        eip_loaded = eip_config.load(eipconfig.get_eipconfig_path(domain))

        launcher = get_vpn_launcher()
        ovpn_path = force_eval(launcher.OPENVPN_BIN_PATH)
        if not os.path.isfile(ovpn_path):
            logger.error("Cannot start OpenVPN, binary not found: %s" %
                         (ovpn_path, ))
            return False

        # check for other problems
        if not eip_loaded or provider_config is None:
            logger.error("Cannot load provider and eip config, cannot "
                         "autostart")
            return False

        client_cert_path = eip_config.\
            get_client_cert_path(provider_config, about_to_download=True)

        if leap_certs.should_redownload(client_cert_path):
            logger.error("The client should redownload the certificate,"
                         " cannot autostart")
            return False

        if not os.path.isfile(client_cert_path):
            logger.error("Can't find the certificate, cannot autostart")
            return False

        return True