mirror of
				https://github.com/Rudd-O/ansible-qubes.git
				synced 2025-11-04 13:38:55 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			78 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			78 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
#!/usr/bin/env python
 | 
						|
 | 
						|
import sys
 | 
						|
import os
 | 
						|
import subprocess
 | 
						|
import socket
 | 
						|
import urllib
 | 
						|
 | 
						|
 | 
						|
def find_hostname_and_command(parms):
 | 
						|
  host = None
 | 
						|
  rest = parms
 | 
						|
  while True:
 | 
						|
    if not rest:
 | 
						|
      break
 | 
						|
    if rest[0] == "--":
 | 
						|
      if host is None:
 | 
						|
        _, host, rest = rest[0], rest[1], rest[2:]
 | 
						|
      else:
 | 
						|
        _, rest = rest[0], rest[1:]
 | 
						|
      break
 | 
						|
    elif rest[0].startswith("-o") and len(rest[0]) > 2:
 | 
						|
      _, rest = rest[0], rest[1:]
 | 
						|
    elif rest[0].startswith("-o"):
 | 
						|
      _, rest = rest[0:1], rest[2:]
 | 
						|
    elif rest[0].startswith("-"):
 | 
						|
      _, rest = rest[0], rest[1:]
 | 
						|
    else:
 | 
						|
      if host is None:
 | 
						|
        host, rest = rest[0], rest[1:]
 | 
						|
      else:
 | 
						|
        break
 | 
						|
  host, port = urllib.splitport(host)
 | 
						|
  return host, rest
 | 
						|
 | 
						|
 | 
						|
def is_qubes_host(host):
 | 
						|
  return host.endswith(".__qubes__")
 | 
						|
 | 
						|
 | 
						|
def get_vmname_and_management_proxy(hostname):
 | 
						|
  host = hostname
 | 
						|
  host = host[:-len(".__qubes__")]
 | 
						|
  if host.endswith("__"):
 | 
						|
    host, proxy, _ = host.rsplit("__", 2)
 | 
						|
    if not host.endswith("."):
 | 
						|
        raise ValueError("invalid proxied host %r" % hostname)
 | 
						|
    return host[:-1], proxy
 | 
						|
  return host, None
 | 
						|
 | 
						|
 | 
						|
parms = sys.argv[1:]
 | 
						|
host, rest = find_hostname_and_command(parms)
 | 
						|
 | 
						|
# SCP execution path.
 | 
						|
if os.path.basename(sys.argv[0]) in ("scp", "qscp"):
 | 
						|
  if not is_qubes_host(host):
 | 
						|
    os.execv("/usr/bin/scp", ["/usr/bin/scp"] + parms)
 | 
						|
 | 
						|
  path_to_ssh = os.path.join(path_to_this_file, "qssh")
 | 
						|
  scmd = ["/usr/bin/scp"] + ["-S", path_to_ssh] + parms
 | 
						|
  os.execvp(scmd[0], scmd)
 | 
						|
 | 
						|
# SSH execution path.
 | 
						|
if not is_qubes_host(host):
 | 
						|
  os.execv("/usr/bin/ssh", ["/usr/bin/ssh"] + parms)
 | 
						|
 | 
						|
path_to_bombshell = os.path.abspath(os.path.join(os.path.dirname(__file__), "bombshell-client"))
 | 
						|
vmname, proxy = get_vmname_and_management_proxy(host)
 | 
						|
if proxy:
 | 
						|
  assert 0, "While connecting to %s (VM name %s): management proxy not supported yet" % (host, vmname)
 | 
						|
 | 
						|
cmd = [
 | 
						|
      path_to_bombshell,
 | 
						|
      vmname,
 | 
						|
  ] + ["sh", "-c", " ".join(rest)]
 | 
						|
os.execvp(cmd[0], cmd)
 |