コード例 #1
0
ファイル: auditconf.py プロジェクト: swipswaps/ssh-audit
    def __setattr__(
            self, name: str, value: Union[str, int, float, bool,
                                          Sequence[int]]) -> None:
        valid = False
        if name in [
                'ssh1', 'ssh2', 'batch', 'client_audit', 'colors', 'verbose',
                'timeout_set', 'json', 'make_policy', 'list_policies'
        ]:
            valid, value = True, bool(value)
        elif name in ['ipv4', 'ipv6']:
            valid = False
            value = bool(value)
            ipv = 4 if name == 'ipv4' else 6
            if value:
                value = tuple(list(self.ipvo) + [ipv])
            else:  # pylint: disable=else-if-used
                if len(self.ipvo) == 0:
                    value = (6, ) if ipv == 4 else (4, )
                else:
                    value = tuple([x for x in self.ipvo if x != ipv])
            self.__setattr__('ipvo', value)
        elif name == 'ipvo':
            if isinstance(value, (tuple, list)):
                uniq_value = Utils.unique_seq(value)
                value = tuple([x for x in uniq_value if x in (4, 6)])
                valid = True
                ipv_both = len(value) == 0
                object.__setattr__(self, 'ipv4', ipv_both or 4 in value)
                object.__setattr__(self, 'ipv6', ipv_both or 6 in value)
        elif name == 'port':
            valid, port = True, Utils.parse_int(value)
            if port < 1 or port > 65535:
                raise ValueError('invalid port: {}'.format(value))
            value = port
        elif name in ['level']:
            if value not in ('info', 'warn', 'fail'):
                raise ValueError('invalid level: {}'.format(value))
            valid = True
        elif name == 'host':
            valid = True
        elif name == 'timeout':
            value = Utils.parse_float(value)
            if value == -1.0:
                raise ValueError('invalid timeout: {}'.format(value))
            valid = True
        elif name in [
                'policy_file', 'policy', 'target_file', 'target_list', 'lookup'
        ]:
            valid = True

        if valid:
            object.__setattr__(self, name, value)
コード例 #2
0
 def _resolve(self,
              ipvo: Sequence[int]) -> Iterable[Tuple[int, Tuple[Any, ...]]]:
     ipvo = tuple([x for x in Utils.unique_seq(ipvo) if x in (4, 6)])
     ipvo_len = len(ipvo)
     prefer_ipvo = ipvo_len > 0
     prefer_ipv4 = prefer_ipvo and ipvo[0] == 4
     if ipvo_len == 1:
         family = socket.AF_INET if ipvo[0] == 4 else socket.AF_INET6
     else:
         family = socket.AF_UNSPEC
     try:
         stype = socket.SOCK_STREAM
         r = socket.getaddrinfo(self.__host, self.__port, family, stype)
         if prefer_ipvo:
             r = sorted(r, key=lambda x: x[0], reverse=not prefer_ipv4)
         check = any(stype == rline[2] for rline in r)
         for af, socktype, _proto, _canonname, addr in r:
             if not check or socktype == socket.SOCK_STREAM:
                 yield af, addr
     except socket.error as e:
         Output().fail('[exception] {}'.format(e))
         sys.exit(exitcodes.CONNECTION_ERROR)