| 1 |
/* |
| 2 |
* This file is part of the go-nft project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
* |
| 16 |
* Copyright 2021 Red Hat, Inc. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
package exec |
| 21 |
|
| 22 |
import ( |
| 23 |
"bytes" |
| 24 |
"fmt" |
| 25 |
"io/ioutil" |
| 26 |
"os" |
| 27 |
"os/exec" |
| 28 |
"strings" |
| 29 |
|
| 30 |
nftconfig "github.com/networkplumbing/go-nft/nft/config" |
| 31 |
) |
| 32 |
|
| 33 |
const ( |
| 34 |
cmdBin = "nft" |
| 35 |
cmdFile = "-f" |
| 36 |
cmdJSON = "-j" |
| 37 |
cmdList = "list" |
| 38 |
cmdRuleset = "ruleset" |
| 39 |
) |
| 40 |
|
| 41 |
// ReadConfig loads the nftables configuration from the system and |
| 42 |
// returns it as a nftables config structure. |
| 43 |
// The system is expected to have the `nft` executable deployed and nftables enabled in the kernel. |
| 44 |
func ReadConfig() (*nftconfig.Config, error) { |
| 45 |
stdout, err := execCommand(cmdJSON, cmdList, cmdRuleset) |
| 46 |
if err != nil { |
| 47 |
return nil, err |
| 48 |
} |
| 49 |
|
| 50 |
config := nftconfig.New() |
| 51 |
if err := config.FromJSON(stdout.Bytes()); err != nil { |
| 52 |
return nil, fmt.Errorf("failed to list ruleset: %v", err) |
| 53 |
} |
| 54 |
|
| 55 |
return config, nil |
| 56 |
} |
| 57 |
|
| 58 |
// ApplyConfig applies the given nftables config on the system. |
| 59 |
// The system is expected to have the `nft` executable deployed and nftables enabled in the kernel. |
| 60 |
func ApplyConfig(c *nftconfig.Config) error { |
| 61 |
data, err := c.ToJSON() |
| 62 |
if err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
|
| 66 |
tmpFile, err := ioutil.TempFile(os.TempDir(), "spoofcheck-") |
| 67 |
if err != nil { |
| 68 |
return fmt.Errorf("failed to create temporary file: %v", err) |
| 69 |
} |
| 70 |
defer os.Remove(tmpFile.Name()) |
| 71 |
|
| 72 |
if _, err = tmpFile.Write(data); err != nil { |
| 73 |
return fmt.Errorf("failed to write to temporary file: %v", err) |
| 74 |
} |
| 75 |
|
| 76 |
if err := tmpFile.Close(); err != nil { |
| 77 |
return fmt.Errorf("failed to close temporary file: %v", err) |
| 78 |
} |
| 79 |
|
| 80 |
if _, err := execCommand(cmdJSON, cmdFile, tmpFile.Name()); err != nil { |
| 81 |
return err |
| 82 |
} |
| 83 |
|
| 84 |
return nil |
| 85 |
} |
| 86 |
|
| 87 |
func execCommand(args ...string) (*bytes.Buffer, error) { |
| 88 |
cmd := exec.Command(cmdBin, args...) |
| 89 |
|
| 90 |
var stdout, stderr bytes.Buffer |
| 91 |
cmd.Stderr = &stderr |
| 92 |
cmd.Stdout = &stdout |
| 93 |
|
| 94 |
if err := cmd.Run(); err != nil { |
| 95 |
return nil, fmt.Errorf( |
| 96 |
"failed to execute %s %s: %v stdout:'%s' stderr:'%s'", |
| 97 |
cmd.Path, strings.Join(cmd.Args, " "), err, stdout.String(), stderr.String(), |
| 98 |
) |
| 99 |
} |
| 100 |
|
| 101 |
return &stdout, nil |
| 102 |
} |