ipfilter.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (C) 2019-2022 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package plugin
  15. import (
  16. "crypto/sha256"
  17. "fmt"
  18. "os/exec"
  19. "github.com/hashicorp/go-hclog"
  20. "github.com/hashicorp/go-plugin"
  21. "github.com/sftpgo/sdk/plugin/ipfilter"
  22. "github.com/drakkan/sftpgo/v2/internal/logger"
  23. )
  24. type ipFilterPlugin struct {
  25. config Config
  26. filter ipfilter.Filter
  27. client *plugin.Client
  28. }
  29. func newIPFilterPlugin(config Config) (*ipFilterPlugin, error) {
  30. p := &ipFilterPlugin{
  31. config: config,
  32. }
  33. if err := p.initialize(); err != nil {
  34. logger.Warn(logSender, "", "unable to create IP filter plugin: %v, config %+v", err, config)
  35. return nil, err
  36. }
  37. return p, nil
  38. }
  39. func (p *ipFilterPlugin) exited() bool {
  40. return p.client.Exited()
  41. }
  42. func (p *ipFilterPlugin) cleanup() {
  43. p.client.Kill()
  44. }
  45. func (p *ipFilterPlugin) initialize() error {
  46. logger.Debug(logSender, "", "create new IP filter plugin %#v", p.config.Cmd)
  47. killProcess(p.config.Cmd)
  48. var secureConfig *plugin.SecureConfig
  49. if p.config.SHA256Sum != "" {
  50. secureConfig.Checksum = []byte(p.config.SHA256Sum)
  51. secureConfig.Hash = sha256.New()
  52. }
  53. client := plugin.NewClient(&plugin.ClientConfig{
  54. HandshakeConfig: ipfilter.Handshake,
  55. Plugins: ipfilter.PluginMap,
  56. Cmd: exec.Command(p.config.Cmd, p.config.Args...),
  57. AllowedProtocols: []plugin.Protocol{
  58. plugin.ProtocolGRPC,
  59. },
  60. AutoMTLS: p.config.AutoMTLS,
  61. SecureConfig: secureConfig,
  62. Managed: false,
  63. Logger: &logger.HCLogAdapter{
  64. Logger: hclog.New(&hclog.LoggerOptions{
  65. Name: fmt.Sprintf("%v.%v", logSender, ipfilter.PluginName),
  66. Level: pluginsLogLevel,
  67. DisableTime: true,
  68. }),
  69. },
  70. })
  71. rpcClient, err := client.Client()
  72. if err != nil {
  73. logger.Debug(logSender, "", "unable to get rpc client for plugin %#v: %v", p.config.Cmd, err)
  74. return err
  75. }
  76. raw, err := rpcClient.Dispense(ipfilter.PluginName)
  77. if err != nil {
  78. logger.Debug(logSender, "", "unable to get plugin %v from rpc client for command %#v: %v",
  79. ipfilter.PluginName, p.config.Cmd, err)
  80. return err
  81. }
  82. p.client = client
  83. p.filter = raw.(ipfilter.Filter)
  84. return nil
  85. }