firewalld.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package iptables
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/Sirupsen/logrus"
  6. "github.com/godbus/dbus"
  7. )
  8. // IPV defines the table string
  9. type IPV string
  10. const (
  11. // Iptables point ipv4 table
  12. Iptables IPV = "ipv4"
  13. // IP6Tables point to ipv6 table
  14. IP6Tables IPV = "ipv6"
  15. // Ebtables point to bridge table
  16. Ebtables IPV = "eb"
  17. )
  18. const (
  19. dbusInterface = "org.fedoraproject.FirewallD1"
  20. dbusPath = "/org/fedoraproject/FirewallD1"
  21. )
  22. // Conn is a connection to firewalld dbus endpoint.
  23. type Conn struct {
  24. sysconn *dbus.Conn
  25. sysobj dbus.BusObject
  26. signal chan *dbus.Signal
  27. }
  28. var (
  29. connection *Conn
  30. firewalldRunning bool // is Firewalld service running
  31. onReloaded []*func() // callbacks when Firewalld has been reloaded
  32. )
  33. // FirewalldInit initializes firewalld management code.
  34. func FirewalldInit() error {
  35. var err error
  36. if connection, err = newConnection(); err != nil {
  37. return fmt.Errorf("Failed to connect to D-Bus system bus: %v", err)
  38. }
  39. firewalldRunning = checkRunning()
  40. if !firewalldRunning {
  41. connection.sysconn.Close()
  42. connection = nil
  43. }
  44. if connection != nil {
  45. go signalHandler()
  46. }
  47. return nil
  48. }
  49. // New() establishes a connection to the system bus.
  50. func newConnection() (*Conn, error) {
  51. c := new(Conn)
  52. if err := c.initConnection(); err != nil {
  53. return nil, err
  54. }
  55. return c, nil
  56. }
  57. // Innitialize D-Bus connection.
  58. func (c *Conn) initConnection() error {
  59. var err error
  60. c.sysconn, err = dbus.SystemBus()
  61. if err != nil {
  62. return err
  63. }
  64. // This never fails, even if the service is not running atm.
  65. c.sysobj = c.sysconn.Object(dbusInterface, dbus.ObjectPath(dbusPath))
  66. rule := fmt.Sprintf("type='signal',path='%s',interface='%s',sender='%s',member='Reloaded'",
  67. dbusPath, dbusInterface, dbusInterface)
  68. c.sysconn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, rule)
  69. rule = fmt.Sprintf("type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged',path='/org/freedesktop/DBus',sender='org.freedesktop.DBus',arg0='%s'",
  70. dbusInterface)
  71. c.sysconn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, rule)
  72. c.signal = make(chan *dbus.Signal, 10)
  73. c.sysconn.Signal(c.signal)
  74. return nil
  75. }
  76. func signalHandler() {
  77. for signal := range connection.signal {
  78. if strings.Contains(signal.Name, "NameOwnerChanged") {
  79. firewalldRunning = checkRunning()
  80. dbusConnectionChanged(signal.Body)
  81. } else if strings.Contains(signal.Name, "Reloaded") {
  82. reloaded()
  83. }
  84. }
  85. }
  86. func dbusConnectionChanged(args []interface{}) {
  87. name := args[0].(string)
  88. oldOwner := args[1].(string)
  89. newOwner := args[2].(string)
  90. if name != dbusInterface {
  91. return
  92. }
  93. if len(newOwner) > 0 {
  94. connectionEstablished()
  95. } else if len(oldOwner) > 0 {
  96. connectionLost()
  97. }
  98. }
  99. func connectionEstablished() {
  100. reloaded()
  101. }
  102. func connectionLost() {
  103. // Doesn't do anything for now. Libvirt also doesn't react to this.
  104. }
  105. // call all callbacks
  106. func reloaded() {
  107. for _, pf := range onReloaded {
  108. (*pf)()
  109. }
  110. }
  111. // OnReloaded add callback
  112. func OnReloaded(callback func()) {
  113. for _, pf := range onReloaded {
  114. if pf == &callback {
  115. return
  116. }
  117. }
  118. onReloaded = append(onReloaded, &callback)
  119. }
  120. // Call some remote method to see whether the service is actually running.
  121. func checkRunning() bool {
  122. var zone string
  123. var err error
  124. if connection != nil {
  125. err = connection.sysobj.Call(dbusInterface+".getDefaultZone", 0).Store(&zone)
  126. return err == nil
  127. }
  128. return false
  129. }
  130. // Passthrough method simply passes args through to iptables/ip6tables
  131. func Passthrough(ipv IPV, args ...string) ([]byte, error) {
  132. var output string
  133. logrus.Debugf("Firewalld passthrough: %s, %s", ipv, args)
  134. if err := connection.sysobj.Call(dbusInterface+".direct.passthrough", 0, ipv, args).Store(&output); err != nil {
  135. return nil, err
  136. }
  137. return []byte(output), nil
  138. }