Ejemplo n.º 1
0
def filter_debug(stream, it):
    """
    Read line chunks from it, either yielding them directly, or building up and
    logging individual lines if they look like SSH debug output.

    This contains the mess of dealing with both line-oriented input, and partial
    lines such as the password prompt.

    Yields `(line, partial)` tuples, where `line` is the line, `partial` is
    :data:`True` if no terminating newline character was present and no more
    data exists in the read buffer. Consuming code can use this to unreliably
    detect the presence of an interactive prompt.
    """
    # The `partial` test is unreliable, but is only problematic when verbosity
    # is enabled: it's possible for a combination of SSH banner, password
    # prompt, verbose output, timing and OS buffering specifics to create a
    # situation where an otherwise newline-terminated line appears to not be
    # terminated, due to a partial read(). If something is broken when
    # ssh_debug_level>0, this is the first place to look.
    state = 'start_of_line'
    buf = b('')
    for chunk in it:
        buf += chunk
        while buf:
            if state == 'start_of_line':
                if len(buf) < 8:
                    # short read near buffer limit, block awaiting at least 8
                    # bytes so we can discern a debug line, or the minimum
                    # interesting token from above or the bootstrap
                    # ('password', 'MITO000\n').
                    break
                elif any(buf.startswith(p) for p in DEBUG_PREFIXES):
                    state = 'in_debug'
                else:
                    state = 'in_plain'
            elif state == 'in_debug':
                if b('\n') not in buf:
                    break
                line, _, buf = bytes_partition(buf, b('\n'))
                LOG.debug('%s: %s', stream.name,
                          mitogen.core.to_text(line.rstrip()))
                state = 'start_of_line'
            elif state == 'in_plain':
                line, nl, buf = bytes_partition(buf, b('\n'))
                yield line + nl, not (nl or buf)
                if nl:
                    state = 'start_of_line'
Ejemplo n.º 2
0
    def _rewrite_source(self, s):
        """
        Mutate the source according to the per-task parameters.
        """
        # While Ansible rewrites the #! using ansible_*_interpreter, it is
        # never actually used to execute the script, instead it is a shell
        # fragment consumed by shell/__init__.py::build_module_command().
        new = [b('#!') + utf8(self.interpreter_fragment)]
        if self.is_python:
            new.append(self.b_ENCODING_STRING)

        _, _, rest = bytes_partition(s, b('\n'))
        new.append(rest)
        return b('\n').join(new)
Ejemplo n.º 3
0
    def _parse(self, fp):
        """
        linux-pam-1.3.1/modules/pam_env/pam_env.c#L207
        """
        for line in fp:
            # '   #export foo=some var  ' -> ['#export', 'foo=some var  ']
            bits = shlex_split_b(line)
            if (not bits) or bits[0].startswith(b('#')):
                continue

            if bits[0] == b('export'):
                bits.pop(0)

            key, sep, value = bytes_partition(b(' ').join(bits), b('='))
            if key and sep:
                yield key, value