Example #1
0
    def dump_device(self, name):

        try:
            # construct args
            args = []
            if System.WS_LINUX == System.name():
                args = ["ip", "addr", "show", name]
            elif System.WS_FREEBSD == System.name():
                args = ["ifconfig", name]
            else:
                raise Exception(
                    "NetDeviceDumper::dump_device - not implemented on system '%s'"
                    % System.name())

            # and run them
            (ret, stdout, stderr) = Command().run(args)
            if ret != 0:
                raise Exception("Invalid exit code: %d; (%s, %s)" %
                                (ret, stdout, stderr))

            # if we got here then it is fine
            return stdout

        except Exception as e:
            return "ERROR: %s\n%s" % (str(e), traceback.format_exc())
Example #2
0
    def configured_tz_linux(self):

        # debug check
        assert(System.name() == System.WS_LINUX)

        # assume default
        value = self.default

        # see if this is centos style
        if Distrib.WS_CENTOS7 == Distrib.name():

            etc_localtime = "/etc/localtime"
            usr_zoneinfo  = "/usr/share/zoneinfo/"

            # see if local time is a link or not
            if os.path.islink(etc_localtime):

                value = os.path.realpath(etc_localtime)
                if value.startswith(usr_zoneinfo):
                    value = value[len(usr_zoneinfo):]

        else:

            # ok this is debian style then
            with open('/etc/timezone') as fin:
                value = fin.read()                
                value = value.strip()

        # return nicely
        return value
Example #3
0
    def set(self, value):

        assert(len(value) > 0)

        # check the provided timezone indeed exists in the pytz
        if value not in pytz.all_timezones:
            raise Exception("Wrong timezone %s (not found pytz.all_timezones)" % value)

        # save the new timezone into the system
        exe  = os.path.join(Paths.bin_dir(), "timezone.py")
        arg1 = "--timezone=%s" % value
        arg2 = "--system=%s" % System.name()
        arg3 = "--distrib=%s" % Distrib.name()
        args = [exe, arg1, arg2, arg3]

        (ret, stdout, stderr) = CommandElevated().run(args)

        # the system zone is set if return value is 0
        if ret == 0:

            # also generate the timezone.setting file
            tz_file = os.path.join(Paths.var_dir(), "console", "console", "timezone.setting")
            with open(tz_file,"w") as fout:
                fout.write(value)

        # and return
        return (ret, stdout, stderr)
Example #4
0
    def get_context_data(self, **kwargs):
        
        context = super(ViewSafetySystemInfo, self).get_context_data(**kwargs)

        v = Version()

        info = {
            'product_name'      : 'Web Safety for Squid Proxy',
            'installed_version' : v.installed,
            'latest_version'    : v.latest,
            'need_to_upgrade'   : v.need_to_upgrade(),        # 0 - no_need_to_upgrade, 1 - may_upgrade, 2 - should_upgrade, 3 - must_upgrade
            'whats_new'         : v.whats_new
        };
        context['info'] = info

        # add hardcoded settings
        context['WEBSAFETY_ETC_DIR']      = Paths.etc_dir()
        context['WEBSAFETY_ETC_DIR_SIZE'] = long(FolderInfo(Paths.etc_dir()).get_size()) 
        context['WEBSAFETY_VAR_DIR']      = Paths.var_dir()
        context['WEBSAFETY_VAR_DIR_SIZE'] = long(FolderInfo(Paths.var_dir()).get_size())
        context['WEBSAFETY_BIN_DIR']      = Paths.bin_dir()
        context['WEBSAFETY_BIN_DIR_SIZE'] = long(FolderInfo(Paths.bin_dir()).get_size())
        context['WEBSAFETY_VERSION']      = Build.version()
        context['WEBSAFETY_ARCH']         = Distrib.arch()
        context['WEBSAFETY_DISTRIB']      = Distrib.name()
        context['WEBSAFETY_SYSTEM']       = System.name()
        
        return context
Example #5
0
    def categorize(self, domain):

        try:
            name = "categories_checker"
            if System.WS_WINDOWS == System.name():
                name += ".exe"

            exe = os.path.join(Paths.bin_dir(), name)
            arg1 = "--definitions=%s" % (os.path.join(
                Paths.var_dir(), "spool", "categories", "definitions.dat"))
            arg2 = "--domain=%s" % domain

            (exit_code, stdout, stderr) = Command().run([exe, arg1, arg2])

            if 0 == exit_code:
                data = stdout.strip()
                data = data.strip("]")
                data = data.strip("[")
                data = data.strip()
                if len(data) == 0:
                    return []
                else:
                    return data.split(':')

        except Exception as e:
            pass

        return []
    def full_path():

        # windows (for debug only)
        if System.name() == System.WS_WINDOWS:
            return r"c:\Squid\bin\squid.exe"

        # centos and redhat
        if os.path.isfile("/etc/centos-release") or os.path.isfile(
                "/etc/redhat-release"):
            return "/usr/sbin/squid"

        # freebsd
        if System.name() == System.WS_FREEBSD:
            return "/usr/local/sbin/squid"

        # by default - debian and ubuntu
        return "/usr/sbin/squid"
    def runas_user():

        if System.WS_WINDOWS == System.name():
            return "unknown"

        if System.WS_FREEBSD == System.name():
            return "squid"

        assert (System.WS_LINUX == System.name())

        if Distrib.WS_CENTOS7 == Distrib.name():
            return "squid"

        if Distrib.name() in [Distrib.WS_DEBIAN9, Distrib.WS_UBUNTU16]:
            return "proxy"

        raise Exception("Unknown distrib '%s' in BinarySquid.runas_user" %
                        Distrib.name())
Example #8
0
    def parse_value(self, str):

        reg_str = r'\s*(\d*.\d*)\s*\%?[usy]+\s*$'
        if System.WS_FREEBSD == System.name():
            reg_str = r'\s*(\d*.\d*)\s*\%?\s.*$'

        match = re.match(reg_str, str)
        if match:
            return float(match.groups()[0])
        return float(0)
Example #9
0
    def is_pfsense(self):
        if System.WS_FREEBSD != System.name():
            return False

        try:
            version = open("/etc/version").readlines()[0]
            match = re.match(r'\d\.\d.*', version)
            if match:
                return True
        except:
            pass
        return False
Example #10
0
    def run(self, icap_port="1344"):

        if System.WS_FREEBSD == System.name():
            args = ["netstat", "-an", "-p", "tcp"]
        else:
            args = [
                "netstat", "--tcp", "--all", "--numeric", "--program",
                "--verbose"
            ]

        # run this command
        (exit_code, stdout, stderr) = Command.run(self, args)

        # and convert
        if exit_code == 0:
            if System.WS_FREEBSD == System.name():
                return self.parse_freebsd_output(stdout, icap_port)
            else:
                return self.parse_linux_output(stdout, icap_port)
        else:
            return []