Beispiel #1
0
 def make_protocol(self):
     #figure out where we read from and write to:
     if self.input_filename=="-":
         #disable stdin buffering:
         self._input = os.fdopen(sys.stdin.fileno(), 'rb', 0)
         setbinarymode(self._input.fileno())
     else:
         self._input = open(self.input_filename, 'rb')
     if self.output_filename=="-":
         #disable stdout buffering:
         self._output = os.fdopen(sys.stdout.fileno(), 'wb', 0)
         setbinarymode(self._output.fileno())
     else:
         self._output = open(self.output_filename, 'wb')
     #stdin and stdout wrapper:
     conn = TwoFileConnection(self._output, self._input, abort_test=None, target=self.name, info=self.name, close_cb=self.net_stop)
     conn.timeout = 0
     protocol = Protocol(gobject, conn, self.process_packet, get_packet_cb=self.get_packet)
     try:
         protocol.enable_encoder("rencode")
     except Exception as e:
         log.warn("failed to enable rencode: %s", e)
         protocol.enable_encoder("bencode")
     protocol.enable_compressor("none")
     protocol.large_packets = self.large_packets
     return protocol
Beispiel #2
0
 def make_protocol(self):
     #figure out where we read from and write to:
     if self.input_filename == "-":
         #disable stdin buffering:
         self._input = os.fdopen(sys.stdin.fileno(), 'rb', 0)
         setbinarymode(self._input.fileno())
     else:
         self._input = open(self.input_filename, 'rb')
     if self.output_filename == "-":
         #disable stdout buffering:
         self._output = os.fdopen(sys.stdout.fileno(), 'wb', 0)
         setbinarymode(self._output.fileno())
     else:
         self._output = open(self.output_filename, 'wb')
     #stdin and stdout wrapper:
     conn = TwoFileConnection(self._output,
                              self._input,
                              abort_test=None,
                              target=self.name,
                              info=self.name,
                              close_cb=self.net_stop)
     conn.timeout = 0
     protocol = Protocol(gobject,
                         conn,
                         self.process_packet,
                         get_packet_cb=self.get_packet)
     try:
         protocol.enable_encoder("rencode")
     except Exception as e:
         log.warn("failed to enable rencode: %s", e)
         protocol.enable_encoder("bencode")
     protocol.enable_compressor("none")
     protocol.large_packets = self.large_packets
     return protocol
Beispiel #3
0
 def make_protocol(self):
     #figure out where we read from and write to:
     if self.input_filename == "-":
         #disable stdin buffering:
         self._input = os.fdopen(sys.stdin.fileno(), 'rb', 0)
         setbinarymode(self._input.fileno())
     else:
         self._input = open(self.input_filename, 'rb')
     if self.output_filename == "-":
         #disable stdout buffering:
         self._output = os.fdopen(sys.stdout.fileno(), 'wb', 0)
         setbinarymode(self._output.fileno())
     else:
         self._output = open(self.output_filename, 'wb')
     #stdin and stdout wrapper:
     conn = TwoFileConnection(self._output,
                              self._input,
                              abort_test=None,
                              target=self.name,
                              socktype=self.name,
                              close_cb=self.net_stop)
     conn.timeout = 0
     protocol = Protocol(self,
                         conn,
                         self.process_packet,
                         get_packet_cb=self.get_packet)
     setup_fastencoder_nocompression(protocol)
     protocol.large_packets = self.large_packets
     return protocol
Beispiel #4
0
 def make_protocol(self):
     #make a connection using the process stdin / stdout
     conn = TwoFileConnection(self.process.stdin, self.process.stdout, abort_test=self.abort_test, target=self.description, socktype=self.description, close_cb=self.subprocess_exit)
     conn.timeout = 0
     protocol = Protocol(self, conn, self.process_packet, get_packet_cb=self.get_packet)
     setup_fastencoder_nocompression(protocol)
     protocol.large_packets = self.large_packets
     return protocol
Beispiel #5
0
 def make_protocol(self):
     #make a connection using the process stdin / stdout
     conn = TwoFileConnection(self.process.stdin, self.process.stdout, abort_test=self.abort_test, target=self.description, socktype=self.description, close_cb=self.subprocess_exit)
     conn.timeout = 0
     protocol = Protocol(self, conn, self.process_packet, get_packet_cb=self.get_packet)
     setup_fastencoder_nocompression(protocol)
     protocol.large_packets = self.large_packets
     return protocol
Beispiel #6
0
 def make_protocol(self):
     #make a connection using the process stdin / stdout
     conn = TwoFileConnection(self.process.stdin, self.process.stdout, abort_test=None, target=self.description, info=self.description, close_cb=self.subprocess_exit)
     conn.timeout = 0
     protocol = Protocol(gobject, conn, self.process_packet, get_packet_cb=self.get_packet)
     #we assume the other end has the same encoders (which is reasonable):
     #TODO: fallback to bencoder
     try:
         protocol.enable_encoder("rencode")
     except Exception as e:
         log.warn("failed to enable rencode: %s", e)
         protocol.enable_encoder("bencode")
     #we assume this is local, so no compression:
     protocol.enable_compressor("none")
     protocol.large_packets = self.large_packets
     return protocol
Beispiel #7
0
 def make_protocol(self):
     #make a connection using the process stdin / stdout
     conn = TwoFileConnection(self.process.stdin,
                              self.process.stdout,
                              abort_test=self.abort_test,
                              target=self.description,
                              socktype=self.description,
                              close_cb=self.subprocess_exit)
     conn.timeout = 0
     protocol = Protocol(self,
                         conn,
                         self.process_packet,
                         get_packet_cb=self.get_packet)
     if LOCAL_ALIASES:
         protocol.send_aliases = LOCAL_SEND_ALIASES
         protocol.receive_aliases = LOCAL_RECEIVE_ALIASES
     setup_fastencoder_nocompression(protocol)
     protocol.large_packets = self.large_packets
     return protocol
Beispiel #8
0
 def make_protocol(self):
     #figure out where we read from and write to:
     if self.input_filename=="-":
         #disable stdin buffering:
         self._input = os.fdopen(sys.stdin.fileno(), 'rb', 0)
         setbinarymode(self._input.fileno())
     else:
         self._input = open(self.input_filename, 'rb')
     if self.output_filename=="-":
         #disable stdout buffering:
         self._output = os.fdopen(sys.stdout.fileno(), 'wb', 0)
         setbinarymode(self._output.fileno())
     else:
         self._output = open(self.output_filename, 'wb')
     #stdin and stdout wrapper:
     conn = TwoFileConnection(self._output, self._input, abort_test=None, target=self.name, socktype=self.name, close_cb=self.net_stop)
     conn.timeout = 0
     protocol = Protocol(self, conn, self.process_packet, get_packet_cb=self.get_packet)
     setup_fastencoder_nocompression(protocol)
     protocol.large_packets = self.large_packets
     return protocol
Beispiel #9
0
 def make_protocol(self):
     #make a connection using the process stdin / stdout
     conn = TwoFileConnection(self.process.stdin,
                              self.process.stdout,
                              abort_test=None,
                              target=self.description,
                              info=self.description,
                              close_cb=self.subprocess_exit)
     conn.timeout = 0
     protocol = Protocol(gobject,
                         conn,
                         self.process_packet,
                         get_packet_cb=self.get_packet)
     #we assume the other end has the same encoders (which is reasonable):
     #TODO: fallback to bencoder
     try:
         protocol.enable_encoder("rencode")
     except Exception as e:
         log.warn("failed to enable rencode: %s", e)
         protocol.enable_encoder("bencode")
     #we assume this is local, so no compression:
     protocol.enable_compressor("none")
     protocol.large_packets = self.large_packets
     return protocol
Beispiel #10
0
def run_proxy(parser, opts, script_file, args, mode):
    from xpra.server.proxy import XpraProxy
    assert "gtk" not in sys.modules
    if mode in ("_proxy_start", "_shadow_start"):
        #we must use a subprocess to avoid messing things up - yuk
        cmd = [script_file]
        if mode=="_proxy_start":
            cmd.append("start")
            assert len(args)==1, "proxy/shadow-start: expected 1 argument but got %s" % len(args)
            display_name = args[0]
        else:
            assert mode=="_shadow_start"
            cmd.append("shadow")
            if len(args)==1:
                #display_name was provided:
                display_name = args[0]
            else:
                display_name = guess_X11_display()
        cmd += args
        if opts.start_child and len(opts.start_child)>0:
            for x in opts.start_child:
                cmd.append("--start-child=%s" % x)
        if opts.exit_with_children:
            cmd.append("--exit-with-children")
        if opts.exit_with_client or mode=="_shadow_start":
            cmd.append("--exit-with-client")
        def setsid():
            os.setsid()
        Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, preexec_fn=setsid)
        dotxpra = DotXpra()
        start = time.time()
        while dotxpra.server_state(display_name, 1)!=DotXpra.LIVE:
            if time.time()-start>5:
                warn("server failed to start after %.1f seconds - sorry!" % (time.time()-start))
                return
            time.sleep(0.10)
    server_conn = connect_or_fail(pick_display(parser, opts, args))
    app = XpraProxy(TwoFileConnection(sys.stdout, sys.stdin, info="stdin/stdout"), server_conn)
    signal.signal(signal.SIGINT, app.quit)
    signal.signal(signal.SIGTERM, app.quit)
    app.run()
    return  0
Beispiel #11
0
def ssh_exec_connect_to(display_desc, opts=None, debug_cb=None, ssh_fail_cb=ssh_connect_failed):
    if not ssh_fail_cb:
        ssh_fail_cb = ssh_connect_failed
    sshpass_command = None
    try:
        cmd = list(display_desc["full_ssh"])
        kwargs = {}
        env = display_desc.get("env")
        kwargs["stderr"] = sys.stderr
        if WIN32:
            from subprocess import CREATE_NEW_PROCESS_GROUP, CREATE_NEW_CONSOLE, STARTUPINFO, STARTF_USESHOWWINDOW
            startupinfo = STARTUPINFO()
            startupinfo.dwFlags |= STARTF_USESHOWWINDOW
            startupinfo.wShowWindow = 0     #aka win32.con.SW_HIDE
            flags = CREATE_NEW_PROCESS_GROUP | CREATE_NEW_CONSOLE
            kwargs.update({
                "startupinfo"   : startupinfo,
                "creationflags" : flags,
                "stderr"        : PIPE,
                })
        elif not display_desc.get("exit_ssh", False) and not OSX:
            kwargs["start_new_session"] = True
        remote_xpra = display_desc["remote_xpra"]
        assert remote_xpra
        socket_dir = display_desc.get("socket_dir")
        proxy_command = display_desc["proxy_command"]       #ie: "_proxy_start"
        display_as_args = display_desc["display_as_args"]   #ie: "--start=xterm :10"
        remote_cmd = ""
        for x in remote_xpra:
            if not remote_cmd:
                check = "if"
            else:
                check = "elif"
            if x=="xpra":
                #no absolute path, so use "which" to check that the command exists:
                pc = ['%s which "%s" > /dev/null 2>&1; then' % (check, x)]
            else:
                pc = ['%s [ -x %s ]; then' % (check, x)]
            pc += [x] + proxy_command + [shellquote(x) for x in display_as_args]
            if socket_dir:
                pc.append("--socket-dir=%s" % socket_dir)
            remote_cmd += " ".join(pc)+";"
        remote_cmd += "else echo \"no run-xpra command found\"; exit 1; fi"
        if INITENV_COMMAND:
            remote_cmd = INITENV_COMMAND + ";" + remote_cmd
        #how many times we need to escape the remote command string
        #depends on how many times the ssh command is parsed
        nssh = sum(int(x=="ssh") for x in cmd)
        if nssh>=2 and MAGIC_QUOTES:
            for _ in range(nssh):
                remote_cmd = shlex.quote(remote_cmd)
        else:
            remote_cmd = "'%s'" % remote_cmd
        cmd.append("sh -c %s" % remote_cmd)
        if debug_cb:
            debug_cb("starting %s tunnel" % str(cmd[0]))
        #non-string arguments can make Popen choke,
        #instead of lazily converting everything to a string, we validate the command:
        for x in cmd:
            if not isinstance(x, str):
                raise InitException("argument is not a string: %s (%s), found in command: %s" % (x, type(x), cmd))
        password = display_desc.get("password")
        if password and not display_desc.get("is_putty", False):
            from xpra.platform.paths import get_sshpass_command
            sshpass_command = get_sshpass_command()
            if sshpass_command:
                #sshpass -e ssh ...
                cmd.insert(0, sshpass_command)
                cmd.insert(1, "-e")
                if env is None:
                    env = os.environ.copy()
                env["SSHPASS"] = password
                #the password will be used by ssh via sshpass,
                #don't try to authenticate again over the ssh-proxy connection,
                #which would trigger warnings if the server does not require
                #authentication over unix-domain-sockets:
                opts.password = None
                del display_desc["password"]
        if env:
            kwargs["env"] = env
        if is_debug_enabled("ssh"):
            log.info("executing ssh command: %s" % (" ".join("\"%s\"" % x for x in cmd)))
        child = Popen(cmd, stdin=PIPE, stdout=PIPE, **kwargs)
    except OSError as e:
        raise InitExit(EXIT_SSH_FAILURE,
                       "Error running ssh command '%s': %s" % (" ".join("\"%s\"" % x for x in cmd), e))
    def abort_test(action):
        """ if ssh dies, we don't need to try to read/write from its sockets """
        e = child.poll()
        if e is not None:
            had_connected = conn.input_bytecount>0 or conn.output_bytecount>0
            if had_connected:
                error_message = "cannot %s using SSH" % action
            else:
                error_message = "SSH connection failure"
            sshpass_error = None
            if sshpass_command:
                sshpass_error = {
                                 1  : "Invalid command line argument",
                                 2  : "Conflicting arguments given",
                                 3  : "General runtime error",
                                 4  : "Unrecognized response from ssh (parse error)",
                                 5  : "Invalid/incorrect password",
                                 6  : "Host public key is unknown. sshpass exits without confirming the new key.",
                                 }.get(e)
                if sshpass_error:
                    error_message += ": %s" % sshpass_error
            if debug_cb:
                debug_cb(error_message)
            if ssh_fail_cb:
                ssh_fail_cb(error_message)
            if "ssh_abort" not in display_desc:
                display_desc["ssh_abort"] = True
                if not had_connected:
                    log.error("Error: SSH connection to the xpra server failed")
                    if sshpass_error:
                        log.error(" %s", sshpass_error)
                    else:
                        log.error(" check your username, hostname, display number, firewall, etc")
                    display_name = display_desc["display_name"]
                    log.error(" for server: %s", display_name)
                else:
                    log.error("The SSH process has terminated with exit code %s", e)
                cmd_info = " ".join(display_desc["full_ssh"])
                log.error(" the command line used was:")
                log.error(" %s", cmd_info)
            raise ConnectionClosedException(error_message) from None
    def stop_tunnel():
        if POSIX:
            #on posix, the tunnel may be shared with other processes
            #so don't kill it... which may leave it behind after use.
            #but at least make sure we close all the pipes:
            for name,fd in {
                            "stdin" : child.stdin,
                            "stdout" : child.stdout,
                            "stderr" : child.stderr,
                            }.items():
                try:
                    if fd:
                        fd.close()
                except Exception as e:
                    print("error closing ssh tunnel %s: %s" % (name, e))
            if not display_desc.get("exit_ssh", False):
                #leave it running
                return
        try:
            if child.poll() is None:
                child.terminate()
        except Exception as e:
            print("error trying to stop ssh tunnel process: %s" % e)
    host = display_desc["host"]
    port = display_desc.get("ssh-port", 22)
    username = display_desc.get("username")
    display = display_desc.get("display")
    info = {
        "host"  : host,
        "port"  : port,
        }
    from xpra.net.bytestreams import TwoFileConnection
    conn = TwoFileConnection(child.stdin, child.stdout,
                             abort_test, target=(host, port),
                             socktype="ssh", close_cb=stop_tunnel, info=info)
    conn.endpoint = host_target_string("ssh", username, host, port, display)
    conn.timeout = 0            #taken care of by abort_test
    conn.process = (child, "ssh", cmd)
    if kwargs.get("stderr")==PIPE:
        def stderr_reader():
            errs = []
            while child.poll() is None:
                try:
                    v = child.stderr.readline()
                except OSError:
                    log("stderr_reader()", exc_info=True)
                    break
                if not v:
                    log("SSH EOF on stderr of %s", cmd)
                    break
                s = bytestostr(v.rstrip(b"\n\r"))
                if s:
                    errs.append(s)
            if errs:
                log.warn("remote SSH stderr:")
                for e in errs:
                    log.warn(" %s", e)
        start_thread(stderr_reader, "ssh-stderr-reader", daemon=True)
    return conn
Beispiel #12
0
            try:
                if child.poll() is None:
                    #only supported on win32 since Python 2.7
                    if hasattr(child, "terminate"):
                        child.terminate()
                    elif hasattr(os, "kill"):
                        os.kill(child.pid, signal.SIGTERM)
                    else:
                        raise Exception(
                            "cannot find function to kill subprocess")
            except Exception, e:
                print("error trying to stop ssh tunnel process: %s" % e)

        return TwoFileConnection(child.stdin,
                                 child.stdout,
                                 abort_test,
                                 target=display_name,
                                 info=dtype,
                                 close_cb=stop_tunnel)

    elif dtype == "unix-domain":
        sockdir = DotXpra(display_desc["socket_dir"])
        sock = socket.socket(socket.AF_UNIX)
        sock.settimeout(5)
        sockfile = sockdir.socket_path(display_desc["display"])
        return _socket_connect(sock, sockfile, display_name, dtype)

    elif dtype == "tcp":
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(10)
        tcp_endpoint = (display_desc["host"], display_desc["port"])
        return _socket_connect(sock, tcp_endpoint, display_name, dtype)