Skip to content

Commit 14f3d00

Browse files
committed
Added user info
1 parent 9ce2fa0 commit 14f3d00

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ def original_dst(sock):
9696

9797

9898
class FirewallClient:
99-
def __init__(self, port, subnets_include, subnets_exclude, dnsport):
99+
def __init__(self, port, subnets_include, subnets_exclude, dnsport, route_username):
100100
self.port = port
101101
self.auto_nets = []
102102
self.subnets_include = subnets_include
103103
self.subnets_exclude = subnets_exclude
104104
self.dnsport = dnsport
105105
argvbase = ([sys.argv[1], sys.argv[0], sys.argv[1]] +
106106
['-v'] * (helpers.verbose or 0) +
107-
['--firewall', str(port), str(dnsport)])
107+
['--firewall', str(port), str(dnsport), '--username', route_username])
108108
if ssyslog._p:
109109
argvbase += ['--syslog']
110110
argv_tries = [
@@ -338,7 +338,7 @@ def onhostlist(hostlist):
338338

339339
def main(listenip, ssh_cmd, remotename, python, latency_control, dns,
340340
seed_hosts, auto_nets,
341-
subnets_include, subnets_exclude, syslog, daemon, pidfile):
341+
subnets_include, subnets_exclude, syslog, daemon, pidfile, route_username):
342342
if syslog:
343343
ssyslog.start_syslog()
344344
if daemon:
@@ -385,7 +385,7 @@ def main(listenip, ssh_cmd, remotename, python, latency_control, dns,
385385
dnsport = 0
386386
dnslistener = None
387387

388-
fw = FirewallClient(listenip[1], subnets_include, subnets_exclude, dnsport)
388+
fw = FirewallClient(listenip[1], subnets_include, subnets_exclude, dnsport, route_username)
389389

390390
try:
391391
return _main(listener, fw, ssh_cmd, remotename,

firewall.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,26 @@ def ipt_ttl(*args):
7070
# multiple copies shouldn't have overlapping subnets, or only the most-
7171
# recently-started one will win (because we use "-I OUTPUT 1" instead of
7272
# "-A OUTPUT").
73-
def do_iptables(port, dnsport, subnets):
73+
def do_iptables(port, dnsport, route_username, subnets):
7474
chain = 'sshuttle-%s' % port
7575

7676
# basic cleanup/setup of chains
7777
if ipt_chain_exists(chain):
78-
nonfatal(ipt, '-D', 'OUTPUT', '-j', chain)
78+
if not route_username:
79+
nonfatal(ipt, '-D', 'OUTPUT', '-j', chain)
80+
else:
81+
nonfatal(ipt, '-m', 'owner', '--uid-owner', route_username, '-D', 'OUTPUT', '-j', chain)
7982
nonfatal(ipt, '-D', 'PREROUTING', '-j', chain)
8083
nonfatal(ipt, '-F', chain)
8184
ipt('-X', chain)
8285

8386
if subnets or dnsport:
8487
ipt('-N', chain)
8588
ipt('-F', chain)
86-
ipt('-I', 'OUTPUT', '1', '-j', chain)
89+
if not route_username:
90+
ipt('-I', 'OUTPUT', '1', '-j', chain)
91+
else:
92+
ipt('-m', 'owner', '--uid-owner', route_username, '-I', 'OUTPUT', '1', '-j', chain)
8793
ipt('-I', 'PREROUTING', '1', '-j', chain)
8894

8995
if subnets:
@@ -255,7 +261,7 @@ def ipfw(*args):
255261
_call(argv)
256262

257263

258-
def do_ipfw(port, dnsport, subnets):
264+
def do_ipfw(port, dnsport, route_username, subnets):
259265
sport = str(port)
260266
xsport = str(port+1)
261267

@@ -451,7 +457,7 @@ def ip_in_subnets(ip, subnets):
451457
# exit. In case that fails, it's not the end of the world; future runs will
452458
# supercede it in the transproxy list, at least, so the leftover rules
453459
# are hopefully harmless.
454-
def main(port, dnsport, syslog):
460+
def main(port, dnsport, syslog, route_username):
455461
assert(port > 0)
456462
assert(port <= 65535)
457463
assert(dnsport >= 0)
@@ -516,7 +522,7 @@ def main(port, dnsport, syslog):
516522
try:
517523
if line:
518524
debug1('firewall manager: starting transproxy.\n')
519-
do_wait = do_it(port, dnsport, subnets)
525+
do_wait = do_it(port, dnsport, route_username, subnets)
520526
sys.stdout.write('STARTED\n')
521527

522528
try:
@@ -546,5 +552,5 @@ def main(port, dnsport, syslog):
546552
debug1('firewall manager: undoing changes.\n')
547553
except:
548554
pass
549-
do_it(port, 0, [])
555+
do_it(port, 0, route_username, [])
550556
restore_etc_hosts(port)

main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def parse_ipport(s):
6767
V,version print sshuttle's version number
6868
syslog send log messages to syslog (default if you use --daemon)
6969
pidfile= pidfile name (only if using --daemon) [./sshuttle.pid]
70+
u,username= route packets only from the specified username (required iptables with -m owner support)
7071
server (internal use only)
7172
firewall (internal use only)
7273
hostwatch (internal use only)
@@ -94,7 +95,7 @@ def parse_ipport(s):
9495
elif opt.firewall:
9596
if len(extra) != 2:
9697
o.fatal('exactly two arguments expected')
97-
sys.exit(firewall.main(int(extra[0]), int(extra[1]), opt.syslog))
98+
sys.exit(firewall.main(int(extra[0]), int(extra[1]), opt.syslog, opt.username))
9899
elif opt.hostwatch:
99100
sys.exit(hostwatch.hw_main(extra))
100101
else:
@@ -128,7 +129,7 @@ def parse_ipport(s):
128129
opt.auto_nets,
129130
parse_subnets(includes),
130131
parse_subnets(excludes),
131-
opt.syslog, opt.daemon, opt.pidfile))
132+
opt.syslog, opt.daemon, opt.pidfile, opt.username))
132133
except FatalNeedsReboot, e:
133134
log('You must reboot before using sshuttle.\n')
134135
sys.exit(EXITCODE_NEEDS_REBOOT)

0 commit comments

Comments
 (0)