|
| 1 | +# vim: set tabstop=4 expandtab : |
| 2 | + |
1 | 3 | import re, errno, socket, select, signal, struct
|
2 | 4 | import compat.ssubprocess as ssubprocess
|
3 | 5 | import helpers, ssyslog
|
@@ -106,17 +108,36 @@ def do_iptables(port, dnsport, route_username, excludedports, subnets):
|
106 | 108 | # to least-specific, and at any given level of specificity, we want
|
107 | 109 | # excludes to come first. That's why the columns are in such a non-
|
108 | 110 | # intuitive order.
|
109 |
| - for swidth,sexclude,snet in sorted(subnets, reverse=True): |
| 111 | + for swidth,sport,sexclude,snet in sorted(subnets, reverse=True): |
110 | 112 | if sexclude:
|
111 |
| - ipt('-A', chain, '-j', 'RETURN', |
112 |
| - '--dest', '%s/%s' % (snet,swidth), |
113 |
| - '-p', 'tcp') |
| 113 | + if sport > 0: |
| 114 | + ipt('-A', chain, '-j', 'RETURN', |
| 115 | + '--dest', '%s/%s' % (snet,swidth), |
| 116 | + '-m', 'tcp', |
| 117 | + '--dport', '%d' % sport, |
| 118 | + '-p', 'tcp') |
| 119 | + else: |
| 120 | + ipt('-A', chain, '-j', 'RETURN', |
| 121 | + '--dest', '%s/%s' % (snet,swidth), |
| 122 | + '-p', 'tcp') |
114 | 123 | else:
|
115 |
| - ipt_ttl('-A', chain, '-j', 'REDIRECT', |
| 124 | + if sport > 0: |
| 125 | + ipt_ttl('-A', chain, '-j', 'REDIRECT', |
116 | 126 | '--dest', '%s/%s' % (snet,swidth),
|
| 127 | + '-m', 'tcp', |
| 128 | + '--dport', '%d' % sport, |
117 | 129 | '-p', 'tcp',
|
| 130 | +<<<<<<< HEAD |
118 | 131 | '--to-ports', str(port),
|
119 | 132 | *eportsargv)
|
| 133 | +======= |
| 134 | + '--to-ports', str(port)) |
| 135 | + else: |
| 136 | + ipt_ttl('-A', chain, '-j', 'REDIRECT', |
| 137 | + '--dest', '%s/%s' % (snet,swidth), |
| 138 | + '-p', 'tcp', |
| 139 | + '--to-ports', str(port)) |
| 140 | +>>>>>>> pull/31 |
120 | 141 |
|
121 | 142 | if dnsport:
|
122 | 143 | for ip in nslist:
|
@@ -327,16 +348,29 @@ def do_ipfw(port, dnsport, route_username, excludedports, subnets):
|
327 | 348 |
|
328 | 349 | if subnets:
|
329 | 350 | # create new subnet entries
|
330 |
| - for swidth,sexclude,snet in sorted(subnets, reverse=True): |
| 351 | + for swidth,dport,sexclude,snet in sorted(subnets, reverse=True): |
331 | 352 | if sexclude:
|
332 |
| - ipfw('add', sport, 'skipto', xsport, |
333 |
| - 'tcp', |
334 |
| - 'from', 'any', 'to', '%s/%s' % (snet,swidth)) |
| 353 | + if dport > 0: |
| 354 | + ipfw('add', sport, 'skipto', xsport, |
| 355 | + 'tcp', |
| 356 | + 'from', 'any', 'to', '%s/%s' % (snet,swidth), |
| 357 | + '%d' % dport) |
| 358 | + else: |
| 359 | + ipfw('add', sport, 'skipto', xsport, |
| 360 | + 'tcp', |
| 361 | + 'from', 'any', 'to', '%s/%s' % (snet,swidth)) |
335 | 362 | else:
|
336 |
| - ipfw('add', sport, 'fwd', '127.0.0.1,%d' % port, |
337 |
| - 'tcp', |
338 |
| - 'from', 'any', 'to', '%s/%s' % (snet,swidth), |
339 |
| - 'not', 'ipttl', '42', 'keep-state', 'setup') |
| 363 | + if dport > 0: |
| 364 | + ipfw('add', sport, 'fwd', '127.0.0.1,%d' % port, |
| 365 | + 'tcp', |
| 366 | + 'from', 'any', 'to', '%s/%s' % (snet,swidth), |
| 367 | + '%d' % dport, |
| 368 | + 'not', 'ipttl', '42', 'keep-state', 'setup') |
| 369 | + else: |
| 370 | + ipfw('add', sport, 'fwd', '127.0.0.1,%d' % port, |
| 371 | + 'tcp', |
| 372 | + 'from', 'any', 'to', '%s/%s' % (snet,swidth), |
| 373 | + 'not', 'ipttl', '42', 'keep-state', 'setup') |
340 | 374 |
|
341 | 375 | # This part is much crazier than it is on Linux, because MacOS (at least
|
342 | 376 | # 10.6, and probably other versions, and maybe FreeBSD too) doesn't
|
@@ -529,10 +563,10 @@ def main(port, dnsport, syslog, route_username, excludedports):
|
529 | 563 | elif line == 'GO\n':
|
530 | 564 | break
|
531 | 565 | try:
|
532 |
| - (width,exclude,ip) = line.strip().split(',', 2) |
| 566 | + (width,dport,exclude,ip) = line.strip().split(',', 3) |
533 | 567 | except:
|
534 | 568 | raise Fatal('firewall: expected route or GO but got %r' % line)
|
535 |
| - subnets.append((int(width), bool(int(exclude)), ip)) |
| 569 | + subnets.append((int(width), int(dport), bool(int(exclude)), ip)) |
536 | 570 |
|
537 | 571 | try:
|
538 | 572 | if line:
|
|
0 commit comments