ffho_netfilter.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #
  2. # FFHO netfilter helper functions
  3. #
  4. def generate_service_rules (services, acls, af):
  5. rules = []
  6. for srv in services:
  7. rule = ""
  8. comment = srv['descr']
  9. # If there are no DST IPs set at all or DST IPs for this AF set, we have a rule to build,
  10. # if this is NOT the case, there is no rule for this AF to generate, carry on.
  11. if not ((not srv['ips']['4'] and not srv['ips']['6']) or srv['ips'][str(af)]):
  12. continue
  13. # Is/are IP(s) set for this service?
  14. if srv['ips'][str(af)]:
  15. rule += "ip" if af == 4 else "ip6"
  16. dst_ips = srv['ips'][str(af)]
  17. if len (dst_ips) == 1:
  18. rule += " daddr %s " % dst_ips[0]
  19. else:
  20. rule += " daddr { %s } " % ", ".join (dst_ips)
  21. # ACL defined for this service?
  22. if srv['acl']:
  23. rule += "ip" if af == 4 else "ip6"
  24. acl = acls[srv['acl']][af]
  25. # Many entries
  26. if type (acl) == list:
  27. rule += " saddr { %s } " % ", ".join (acl)
  28. else:
  29. rule += " saddr %s " % acl
  30. comment += " (acl: %s)" % srv['acl']
  31. # Multiple ports?
  32. if len (srv['ports']) > 1:
  33. ports = "{ %s }" % ", ".join (map (str, sorted (srv['ports'])))
  34. else:
  35. ports = srv['ports'][0]
  36. rule += "%s dport %s counter accept comment \"%s\"" % (srv['proto'], ports, comment)
  37. rules.append (rule)
  38. return rules