| 1 | // Copyright 2018 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | "github.com/godbus/dbus/v5" |
| 22 | |
| 23 | current "github.com/containernetworking/cni/pkg/types/100" |
| 24 | ) |
| 25 | |
| 26 | const ( |
| 27 | dbusName = "org.freedesktop.DBus" |
| 28 | dbusPath = "/org/freedesktop/DBus" |
| 29 | dbusGetNameOwnerMethod = "GetNameOwner" |
| 30 | |
| 31 | firewalldName = "org.fedoraproject.FirewallD1" |
| 32 | firewalldPath = "/org/fedoraproject/FirewallD1" |
| 33 | firewalldZoneInterface = "org.fedoraproject.FirewallD1.zone" |
| 34 | firewalldAddSourceMethod = "addSource" |
| 35 | firewalldRemoveSourceMethod = "removeSource" |
| 36 | firewalldQuerySourceMethod = "querySource" |
| 37 | |
| 38 | errZoneAlreadySet = "ZONE_ALREADY_SET" |
| 39 | ) |
| 40 | |
| 41 | // Only used for testcases to override the D-Bus connection |
| 42 | var testConn *dbus.Conn |
| 43 | |
| 44 | type fwdBackend struct { |
| 45 | conn *dbus.Conn |
| 46 | } |
| 47 | |
| 48 | // fwdBackend implements the FirewallBackend interface |
| 49 | var _ FirewallBackend = &fwdBackend{} |
| 50 | |
| 51 | func getConn() (*dbus.Conn, error) { |
| 52 | if testConn != nil { |
| 53 | return testConn, nil |
| 54 | } |
| 55 | return dbus.SystemBus() |
| 56 | } |
| 57 | |
| 58 | // isFirewalldRunning checks whether firewalld is running. |
| 59 | func isFirewalldRunning() bool { |
| 60 | conn, err := getConn() |
| 61 | if err != nil { |
| 62 | return false |
| 63 | } |
| 64 | |
| 65 | dbusObj := conn.Object(dbusName, dbusPath) |
| 66 | var res string |
| 67 | if err := dbusObj.Call(dbusName+"."+dbusGetNameOwnerMethod, 0, firewalldName).Store(&res); err != nil { |
| 68 | return false |
| 69 | } |
| 70 | |
| 71 | return true |
| 72 | } |
| 73 | |
| 74 | func newFirewalldBackend() (FirewallBackend, error) { |
| 75 | conn, err := getConn() |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | |
| 80 | backend := &fwdBackend{ |
| 81 | conn: conn, |
| 82 | } |
| 83 | return backend, nil |
| 84 | } |
| 85 | |
| 86 | func (fb *fwdBackend) Add(conf *FirewallNetConf, result *current.Result) error { |
| 87 | for _, ip := range result.IPs { |
| 88 | ipStr := ipString(ip.Address) |
| 89 | // Add a firewalld rule which assigns the given source IP to the given zone |
| 90 | firewalldObj := fb.conn.Object(firewalldName, firewalldPath) |
| 91 | var res string |
| 92 | if err := firewalldObj.Call(firewalldZoneInterface+"."+firewalldAddSourceMethod, 0, conf.FirewalldZone, ipStr).Store(&res); err != nil { |
| 93 | if !strings.Contains(err.Error(), errZoneAlreadySet) { |
| 94 | return fmt.Errorf("failed to add the address %v to %v zone: %v", ipStr, conf.FirewalldZone, err) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return nil |
| 99 | } |
| 100 | |
| 101 | func (fb *fwdBackend) Del(conf *FirewallNetConf, result *current.Result) error { |
| 102 | for _, ip := range result.IPs { |
| 103 | ipStr := ipString(ip.Address) |
| 104 | // Remove firewalld rules which assigned the given source IP to the given zone |
| 105 | firewalldObj := fb.conn.Object(firewalldName, firewalldPath) |
| 106 | var res string |
| 107 | firewalldObj.Call(firewalldZoneInterface+"."+firewalldRemoveSourceMethod, 0, conf.FirewalldZone, ipStr).Store(&res) |
| 108 | } |
| 109 | return nil |
| 110 | } |
| 111 | |
| 112 | func (fb *fwdBackend) Check(conf *FirewallNetConf, result *current.Result) error { |
| 113 | for _, ip := range result.IPs { |
| 114 | ipStr := ipString(ip.Address) |
| 115 | // Check for a firewalld rule for the given source IP to the given zone |
| 116 | firewalldObj := fb.conn.Object(firewalldName, firewalldPath) |
| 117 | var res bool |
| 118 | if err := firewalldObj.Call(firewalldZoneInterface+"."+firewalldQuerySourceMethod, 0, conf.FirewalldZone, ipStr).Store(&res); err != nil { |
| 119 | return fmt.Errorf("failed to find the address %v in %v zone", ipStr, conf.FirewalldZone) |
| 120 | } |
| 121 | } |
| 122 | return nil |
| 123 | } |