From cfaa96b82c9a6ea19beeaef99b1a820dee4201fb Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Sun, 23 Apr 2017 08:24:12 -0400 Subject: [PATCH] add feed policy for inbound feeds in default section of feeds.ini --- contrib/backends/srndv2/src/srnd/config.go | 51 ++++++++++++++-------- contrib/backends/srndv2/src/srnd/daemon.go | 5 ++- contrib/backends/srndv2/src/srnd/nntp.go | 4 +- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/contrib/backends/srndv2/src/srnd/config.go b/contrib/backends/srndv2/src/srnd/config.go index a7515c3..9eca5fa 100644 --- a/contrib/backends/srndv2/src/srnd/config.go +++ b/contrib/backends/srndv2/src/srnd/config.go @@ -58,17 +58,18 @@ type HookConfig struct { } type SRNdConfig struct { - daemon map[string]string - crypto *CryptoConfig - store map[string]string - database map[string]string - cache map[string]string - feeds []FeedConfig - frontend map[string]string - system map[string]string - worker map[string]string - pprof *ProfilingConfig - hooks []*HookConfig + daemon map[string]string + crypto *CryptoConfig + store map[string]string + database map[string]string + cache map[string]string + feeds []FeedConfig + frontend map[string]string + system map[string]string + worker map[string]string + pprof *ProfilingConfig + hooks []*HookConfig + inboundPolicy *FeedPolicy } // check for config files @@ -219,8 +220,16 @@ func GenSRNdConfig() *configparser.Configuration { } // save a list of feeds to overwrite feeds.ini -func SaveFeeds(feeds []FeedConfig) (err error) { +func SaveFeeds(feeds []FeedConfig, inboundPolicy *FeedPolicy) (err error) { conf := configparser.NewConfiguration() + + if inboundPolicy != nil { + s := conf.NewSection("") + for k, v := range inboundPolicy.rules { + s.Add(k, v) + } + } + for _, feed := range feeds { if len(feed.Name) == 0 { // don't do feed with no name @@ -381,8 +390,8 @@ func ReadConfig() *SRNdConfig { fname = os.Getenv("SRND_FEEDS_INI_PATH") } } - - confs, err := feedParse(fname) + var confs []FeedConfig + confs, sconf.inboundPolicy, err = feedParse(fname) if err != nil { log.Fatal("failed to parse", fname, err) } @@ -399,7 +408,7 @@ func ReadConfig() *SRNdConfig { if err == nil { for _, f := range feeds { log.Println("load feed", f) - confs, err := feedParse(f) + confs, _, err := feedParse(f) if err != nil { log.Fatal("failed to parse feed", f, err) } @@ -411,12 +420,20 @@ func ReadConfig() *SRNdConfig { return &sconf } -func feedParse(fname string) (confs []FeedConfig, err error) { +func feedParse(fname string) (confs []FeedConfig, inboundPolicy *FeedPolicy, err error) { conf, err := configparser.Read(fname) if err != nil { - return nil, err + return + } + + default_sect, err := conf.Section("") + if err == nil { + opts := default_sect.Options() + inboundPolicy = &FeedPolicy{ + rules: opts, + } } sections, err := conf.Find("feed-*") diff --git a/contrib/backends/srndv2/src/srnd/daemon.go b/contrib/backends/srndv2/src/srnd/daemon.go index 688bc3a..60742be 100644 --- a/contrib/backends/srndv2/src/srnd/daemon.go +++ b/contrib/backends/srndv2/src/srnd/daemon.go @@ -295,7 +295,7 @@ func (self *NNTPDaemon) storeFeedsConfig() (err error) { for _, status := range feeds { feedconfigs = append(feedconfigs, *status.State.Config) } - err = SaveFeeds(feedconfigs) + err = SaveFeeds(feedconfigs, self.conf.inboundPolicy) return } @@ -410,7 +410,7 @@ func (self *NNTPDaemon) persistFeed(conf *FeedConfig, mode string, n int) { continue } nntp := createNNTPConnection(conf.Addr) - nntp.policy = conf.policy + nntp.policy = &conf.policy nntp.feedname = conf.Name nntp.name = fmt.Sprintf("%s-%d-%s", conf.Name, n, mode) stream, reader, use_tls, err := nntp.outboundHandshake(textproto.NewConn(conn), conf) @@ -948,6 +948,7 @@ func (self *NNTPDaemon) acceptloop() { } addr := conn.RemoteAddr() nntp.name = fmt.Sprintf("%s-inbound-feed", addr.String()) + nntp.policy = self.conf.inboundPolicy c := textproto.NewConn(conn) // send banners and shit err = nntp.inboundHandshake(c) diff --git a/contrib/backends/srndv2/src/srnd/nntp.go b/contrib/backends/srndv2/src/srnd/nntp.go index 4ec81e8..e6dafc0 100644 --- a/contrib/backends/srndv2/src/srnd/nntp.go +++ b/contrib/backends/srndv2/src/srnd/nntp.go @@ -61,7 +61,7 @@ type nntpConnection struct { // what article is currently selected selected_article string // the policy for federation - policy FeedPolicy + policy *FeedPolicy // lock help when expecting non pipelined activity access sync.Mutex @@ -439,7 +439,7 @@ func (self *nntpConnection) checkMIMEHeaderNoAuth(daemon *NNTPDaemon, hdr textpr reason = "poster's pubkey is banned" ban = true return - } else if !self.policy.AllowsNewsgroup(newsgroup) { + } else if self.policy != nil && !self.policy.AllowsNewsgroup(newsgroup) { reason = "newsgroup not allowed by feed policy" ban = true return