-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.py
executable file
·84 lines (68 loc) · 2.28 KB
/
ssh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python3
import json
import os
import pwd
import psutil
import sys
from auth_api_client import config
from auth_api_client.common import get_ssh_keys, load_config, log_error, get_ssh_key_extra_options
CMD_MAP = {
"rsync": "/usr/bin/rrsync",
"rsync_ro": "/usr/bin/rrsync -ro",
"rsync_wo": "/usr/bin/rrsync -wo",
"sftp": "internal-sftp",
"sftp_ro": "internal-sftp -R"
}
CMD_BOGUS = "/usr/sbin/nologin"
load_config()
ppid = os.getppid()
p_sshd = psutil.Process(ppid)
if p_sshd.exe() != "/usr/sbin/sshd":
log_error("Parent process is not sshd")
sys.exit(1)
if len(sys.argv) < 2:
log_error("No user specified")
sys.exit(1)
try:
user = pwd.getpwnam(sys.argv[1])
except Exception as e:
log_error("Invalid user specified")
sys.exit(1)
remote_ip = None
for conn in psutil.net_connections(kind="tcp"):
if conn.laddr[1] == 22 and conn.status == psutil.CONN_ESTABLISHED:
proc = psutil.Process(conn.pid)
if proc.ppid() == ppid and proc.username() == "sshd":
remote_ip = conn.raddr[0]
break
if not remote_ip:
log_error("Cannot determine remote IP address")
sys.exit(1)
# Drop root privileges no longer required
pwentry = pwd.getpwnam(config.config["run_as"])
os.setgid(pwentry.pw_gid)
os.setgroups([])
os.setuid(pwentry.pw_uid)
for key in get_ssh_keys(user.pw_name, remote_ip, ppid):
restrictions = []
if key["allowed_ips"]:
try:
allowed_ips = ",".join(json.loads(key["allowed_ips"]))
except: # Play it safe and don't allow key if invalid allowed_ips
continue
restrictions.append("from=\"%s\"" % allowed_ips)
if key["access_type"] != "any":
restrictions.append("restrict")
if key["access_type"] in CMD_MAP:
command = CMD_MAP[key["access_type"]]
else:
command = CMD_BOGUS
if key["access_type"].startswith("rsync"):
extra_options = get_ssh_key_extra_options(key)
if "rsync_directory" in extra_options:
command += " %s" % extra_options["rsync_directory"]
else:
command += " /"
restrictions.append("command=\"%s\"" % command)
print((" ".join([",".join(restrictions), key["type"], key["pub_key"]])).strip())
sys.stdout.flush()