firewalld.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //go:build linux
  2. // +build linux
  3. package iptables
  4. import (
  5. "fmt"
  6. "strings"
  7. dbus "github.com/godbus/dbus/v5"
  8. "github.com/sirupsen/logrus"
  9. )
  10. // IPV defines the table string
  11. type IPV string
  12. const (
  13. // Iptables point ipv4 table
  14. Iptables IPV = "ipv4"
  15. // IP6Tables point to ipv6 table
  16. IP6Tables IPV = "ipv6"
  17. // Ebtables point to bridge table
  18. Ebtables IPV = "eb"
  19. )
  20. const (
  21. dbusInterface = "org.fedoraproject.FirewallD1"
  22. dbusPath = "/org/fedoraproject/FirewallD1"
  23. dbusConfigPath = "/org/fedoraproject/FirewallD1/config"
  24. dockerZone = "docker"
  25. )
  26. // Conn is a connection to firewalld dbus endpoint.
  27. type Conn struct {
  28. sysconn *dbus.Conn
  29. sysObj dbus.BusObject
  30. sysConfObj dbus.BusObject
  31. signal chan *dbus.Signal
  32. }
  33. // ZoneSettings holds the firewalld zone settings, documented in
  34. // https://firewalld.org/documentation/man-pages/firewalld.dbus.html
  35. type ZoneSettings struct {
  36. version string
  37. name string
  38. description string
  39. unused bool
  40. target string
  41. services []string
  42. ports [][]interface{}
  43. icmpBlocks []string
  44. masquerade bool
  45. forwardPorts [][]interface{}
  46. interfaces []string
  47. sourceAddresses []string
  48. richRules []string
  49. protocols []string
  50. sourcePorts [][]interface{}
  51. icmpBlockInversion bool
  52. }
  53. var (
  54. connection *Conn
  55. firewalldRunning bool // is Firewalld service running
  56. onReloaded []*func() // callbacks when Firewalld has been reloaded
  57. )
  58. // FirewalldInit initializes firewalld management code.
  59. func FirewalldInit() error {
  60. var err error
  61. if connection, err = newConnection(); err != nil {
  62. return fmt.Errorf("Failed to connect to D-Bus system bus: %v", err)
  63. }
  64. firewalldRunning = checkRunning()
  65. if !firewalldRunning {
  66. connection.sysconn.Close()
  67. connection = nil
  68. }
  69. if connection != nil {
  70. go signalHandler()
  71. if err := setupDockerZone(); err != nil {
  72. return err
  73. }
  74. }
  75. return nil
  76. }
  77. // New() establishes a connection to the system bus.
  78. func newConnection() (*Conn, error) {
  79. c := new(Conn)
  80. if err := c.initConnection(); err != nil {
  81. return nil, err
  82. }
  83. return c, nil
  84. }
  85. // Initialize D-Bus connection.
  86. func (c *Conn) initConnection() error {
  87. var err error
  88. c.sysconn, err = dbus.SystemBus()
  89. if err != nil {
  90. return err
  91. }
  92. // This never fails, even if the service is not running atm.
  93. c.sysObj = c.sysconn.Object(dbusInterface, dbus.ObjectPath(dbusPath))
  94. c.sysConfObj = c.sysconn.Object(dbusInterface, dbus.ObjectPath(dbusConfigPath))
  95. rule := fmt.Sprintf("type='signal',path='%s',interface='%s',sender='%s',member='Reloaded'",
  96. dbusPath, dbusInterface, dbusInterface)
  97. c.sysconn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, rule)
  98. rule = fmt.Sprintf("type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged',path='/org/freedesktop/DBus',sender='org.freedesktop.DBus',arg0='%s'",
  99. dbusInterface)
  100. c.sysconn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, rule)
  101. c.signal = make(chan *dbus.Signal, 10)
  102. c.sysconn.Signal(c.signal)
  103. return nil
  104. }
  105. func signalHandler() {
  106. for signal := range connection.signal {
  107. if strings.Contains(signal.Name, "NameOwnerChanged") {
  108. firewalldRunning = checkRunning()
  109. dbusConnectionChanged(signal.Body)
  110. } else if strings.Contains(signal.Name, "Reloaded") {
  111. reloaded()
  112. }
  113. }
  114. }
  115. func dbusConnectionChanged(args []interface{}) {
  116. name := args[0].(string)
  117. oldOwner := args[1].(string)
  118. newOwner := args[2].(string)
  119. if name != dbusInterface {
  120. return
  121. }
  122. if len(newOwner) > 0 {
  123. connectionEstablished()
  124. } else if len(oldOwner) > 0 {
  125. connectionLost()
  126. }
  127. }
  128. func connectionEstablished() {
  129. reloaded()
  130. }
  131. func connectionLost() {
  132. // Doesn't do anything for now. Libvirt also doesn't react to this.
  133. }
  134. // call all callbacks
  135. func reloaded() {
  136. for _, pf := range onReloaded {
  137. (*pf)()
  138. }
  139. }
  140. // OnReloaded add callback
  141. func OnReloaded(callback func()) {
  142. for _, pf := range onReloaded {
  143. if pf == &callback {
  144. return
  145. }
  146. }
  147. onReloaded = append(onReloaded, &callback)
  148. }
  149. // Call some remote method to see whether the service is actually running.
  150. func checkRunning() bool {
  151. var zone string
  152. var err error
  153. if connection != nil {
  154. err = connection.sysObj.Call(dbusInterface+".getDefaultZone", 0).Store(&zone)
  155. return err == nil
  156. }
  157. return false
  158. }
  159. // Passthrough method simply passes args through to iptables/ip6tables
  160. func Passthrough(ipv IPV, args ...string) ([]byte, error) {
  161. var output string
  162. logrus.Debugf("Firewalld passthrough: %s, %s", ipv, args)
  163. if err := connection.sysObj.Call(dbusInterface+".direct.passthrough", 0, ipv, args).Store(&output); err != nil {
  164. return nil, err
  165. }
  166. return []byte(output), nil
  167. }
  168. // getDockerZoneSettings converts the ZoneSettings struct into a interface slice
  169. func getDockerZoneSettings() []interface{} {
  170. settings := ZoneSettings{
  171. version: "1.0",
  172. name: dockerZone,
  173. description: "zone for docker bridge network interfaces",
  174. target: "ACCEPT",
  175. }
  176. slice := []interface{}{
  177. settings.version,
  178. settings.name,
  179. settings.description,
  180. settings.unused,
  181. settings.target,
  182. settings.services,
  183. settings.ports,
  184. settings.icmpBlocks,
  185. settings.masquerade,
  186. settings.forwardPorts,
  187. settings.interfaces,
  188. settings.sourceAddresses,
  189. settings.richRules,
  190. settings.protocols,
  191. settings.sourcePorts,
  192. settings.icmpBlockInversion,
  193. }
  194. return slice
  195. }
  196. // setupDockerZone creates a zone called docker in firewalld which includes docker interfaces to allow
  197. // container networking
  198. func setupDockerZone() error {
  199. var zones []string
  200. // Check if zone exists
  201. if err := connection.sysObj.Call(dbusInterface+".zone.getZones", 0).Store(&zones); err != nil {
  202. return err
  203. }
  204. if contains(zones, dockerZone) {
  205. logrus.Infof("Firewalld: %s zone already exists, returning", dockerZone)
  206. return nil
  207. }
  208. logrus.Debugf("Firewalld: creating %s zone", dockerZone)
  209. settings := getDockerZoneSettings()
  210. // Permanent
  211. if err := connection.sysConfObj.Call(dbusInterface+".config.addZone", 0, dockerZone, settings).Err; err != nil {
  212. return err
  213. }
  214. // Reload for change to take effect
  215. if err := connection.sysObj.Call(dbusInterface+".reload", 0).Err; err != nil {
  216. return err
  217. }
  218. return nil
  219. }
  220. // AddInterfaceFirewalld adds the interface to the trusted zone
  221. func AddInterfaceFirewalld(intf string) error {
  222. var intfs []string
  223. // Check if interface is already added to the zone
  224. if err := connection.sysObj.Call(dbusInterface+".zone.getInterfaces", 0, dockerZone).Store(&intfs); err != nil {
  225. return err
  226. }
  227. // Return if interface is already part of the zone
  228. if contains(intfs, intf) {
  229. logrus.Infof("Firewalld: interface %s already part of %s zone, returning", intf, dockerZone)
  230. return nil
  231. }
  232. logrus.Debugf("Firewalld: adding %s interface to %s zone", intf, dockerZone)
  233. // Runtime
  234. if err := connection.sysObj.Call(dbusInterface+".zone.addInterface", 0, dockerZone, intf).Err; err != nil {
  235. return err
  236. }
  237. return nil
  238. }
  239. // DelInterfaceFirewalld removes the interface from the trusted zone
  240. func DelInterfaceFirewalld(intf string) error {
  241. var intfs []string
  242. // Check if interface is part of the zone
  243. if err := connection.sysObj.Call(dbusInterface+".zone.getInterfaces", 0, dockerZone).Store(&intfs); err != nil {
  244. return err
  245. }
  246. // Remove interface if it exists
  247. if !contains(intfs, intf) {
  248. return fmt.Errorf("Firewalld: unable to find interface %s in %s zone", intf, dockerZone)
  249. }
  250. logrus.Debugf("Firewalld: removing %s interface from %s zone", intf, dockerZone)
  251. // Runtime
  252. if err := connection.sysObj.Call(dbusInterface+".zone.removeInterface", 0, dockerZone, intf).Err; err != nil {
  253. return err
  254. }
  255. return nil
  256. }
  257. func contains(list []string, val string) bool {
  258. for _, v := range list {
  259. if v == val {
  260. return true
  261. }
  262. }
  263. return false
  264. }