From 12536e329b4fb8d27e1e2a336e783ae0fc046e2f Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Thu, 19 Jan 2017 23:23:08 +0300 Subject: [PATCH] nncp-mincfg command --- common.mk | 4 + doc/cmds.texi | 15 ++++ src/cypherpunks.ru/nncp/cfg.go | 21 +++-- src/cypherpunks.ru/nncp/cmd/nncp-call/main.go | 3 + .../nncp/cmd/nncp-caller/main.go | 3 + .../nncp/cmd/nncp-daemon/main.go | 3 + src/cypherpunks.ru/nncp/cmd/nncp-file/main.go | 3 + src/cypherpunks.ru/nncp/cmd/nncp-freq/main.go | 3 + src/cypherpunks.ru/nncp/cmd/nncp-mail/main.go | 3 + .../nncp/cmd/nncp-mincfg/main.go | 89 +++++++++++++++++++ .../nncp/cmd/nncp-newnode/main.go | 2 +- src/cypherpunks.ru/nncp/cmd/nncp-pkt/main.go | 3 + src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go | 3 + src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go | 4 +- src/cypherpunks.ru/nncp/ctx.go | 7 +- src/cypherpunks.ru/nncp/toss.go | 4 +- src/cypherpunks.ru/nncp/toss_test.go | 5 ++ src/cypherpunks.ru/nncp/tx_test.go | 1 + 18 files changed, 161 insertions(+), 15 deletions(-) create mode 100644 src/cypherpunks.ru/nncp/cmd/nncp-mincfg/main.go diff --git a/common.mk b/common.mk index c05ae89..0f7345e 100644 --- a/common.mk +++ b/common.mk @@ -25,6 +25,7 @@ ALL = \ nncp-file \ nncp-freq \ nncp-log \ + nncp-mincfg \ nncp-newnode \ nncp-pkt \ nncp-stat \ @@ -57,6 +58,9 @@ nncp-log: nncp-mail: GOPATH=$(GOPATH) go build -ldflags "$(LDFLAGS)" cypherpunks.ru/nncp/cmd/nncp-mail +nncp-mincfg: + GOPATH=$(GOPATH) go build -ldflags "$(LDFLAGS)" cypherpunks.ru/nncp/cmd/nncp-mincfg + nncp-newnode: GOPATH=$(GOPATH) go build -ldflags "$(LDFLAGS)" cypherpunks.ru/nncp/cmd/nncp-newnode diff --git a/doc/cmds.texi b/doc/cmds.texi index 4c1873f..2751006 100644 --- a/doc/cmds.texi +++ b/doc/cmds.texi @@ -166,6 +166,18 @@ side will execute specified @ref{CfgSendmail, sendmail} command with @option{USER}s appended as a command line argument and feed decompressed mail body to that command's stdin. +@node nncp-mincfg +@section nncp-mincfg + +@verbatim +% nncp-mincfg [options] > stripped.yaml +@end verbatim + +Print out stripped configuration version: only path to @ref{Spool, +spool}, path to log file, neighbours public keys are stayed. This is +useful mainly for usage with @ref{nncp-xfer} that has to know only +neighbours, without private keys involving. + @node nncp-newnode @section nncp-newnode @@ -270,6 +282,9 @@ remove them. @option{-rx} option tells only to move inbound packets addressed to us. @option{-tx} option tells exactly the opposite: move only outbound packets. +@ref{nncp-mincfg} could be useful for creating stripped minimalistic +configuration file version without any private keys. + @file{DIR} directory has the following structure: @file{RECIPIENT/SENDER/PACKET}, where @file{RECIPIENT} is Base32 encoded destination node, @file{SENDER} is Base32 encoded sender node. diff --git a/src/cypherpunks.ru/nncp/cfg.go b/src/cypherpunks.ru/nncp/cfg.go index c05a6a7..f89c106 100644 --- a/src/cypherpunks.ru/nncp/cfg.go +++ b/src/cypherpunks.ru/nncp/cfg.go @@ -43,8 +43,8 @@ type NodeYAML struct { Id string ExchPub string SignPub string - NoisePub *string `noisepub,omitempty` - Sendmail []string + NoisePub *string `noisepub,omitempty` + Sendmail []string `sendmail,omitempty` Incoming *string `incoming,omitempty` Freq *string `freq,omitempty` Via []string `via,omitempty` @@ -86,7 +86,7 @@ type NotifyYAML struct { } type CfgYAML struct { - Self NodeOurYAML + Self *NodeOurYAML `self,omitempty` Neigh map[string]NodeYAML Spool string @@ -231,7 +231,7 @@ func NewNode(name string, yml NodeYAML) (*Node, error) { return &node, nil } -func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) { +func NewNodeOur(yml *NodeOurYAML) (*NodeOur, error) { id, err := NodeIdFromString(yml.Id) if err != nil { return nil, err @@ -324,9 +324,15 @@ func CfgParse(data []byte) (*Ctx, error) { if err != nil { return nil, err } - self, err := NewNodeOur(cfgYAML.Self) - if err != nil { - return nil, err + if _, exists := cfgYAML.Neigh["self"]; !exists { + return nil, errors.New("self neighbour missing") + } + var self *NodeOur + if cfgYAML.Self != nil { + self, err = NewNodeOur(cfgYAML.Self) + if err != nil { + return nil, err + } } spoolPath := path.Clean(cfgYAML.Spool) if !path.IsAbs(spoolPath) { @@ -364,6 +370,7 @@ func CfgParse(data []byte) (*Ctx, error) { ctx.Alias[name] = neigh.Id vias[*neigh.Id] = neighYAML.Via } + ctx.SelfId = ctx.Alias["self"] for neighId, viasRaw := range vias { for _, viaRaw := range viasRaw { foundNodeId, err := ctx.FindNode(viaRaw) diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go index 03005a1..9694639 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go @@ -82,6 +82,9 @@ func main() { if err != nil { log.Fatalln("Can not parse config:", err) } + if ctx.Self == nil { + log.Fatalln("Config lacks private keys") + } ctx.Quiet = *quiet ctx.Debug = *debug diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-caller/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-caller/main.go index bcfd6c3..3cbbe8f 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-caller/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-caller/main.go @@ -67,6 +67,9 @@ func main() { if err != nil { log.Fatalln("Can not parse config:", err) } + if ctx.Self == nil { + log.Fatalln("Config lacks private keys") + } ctx.Quiet = *quiet ctx.Debug = *debug diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-daemon/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-daemon/main.go index c1355c8..b3678cd 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-daemon/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-daemon/main.go @@ -73,6 +73,9 @@ func main() { if err != nil { log.Fatalln("Can not parse config:", err) } + if ctx.Self == nil { + log.Fatalln("Config lacks private keys") + } ctx.Quiet = *quiet ctx.Debug = *debug diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-file/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-file/main.go index f677286..3291c3a 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-file/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-file/main.go @@ -74,6 +74,9 @@ func main() { if err != nil { log.Fatalln("Can not parse config:", err) } + if ctx.Self == nil { + log.Fatalln("Config lacks private keys") + } ctx.Quiet = *quiet ctx.Debug = *debug diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-freq/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-freq/main.go index ada8edb..2641da0 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-freq/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-freq/main.go @@ -74,6 +74,9 @@ func main() { if err != nil { log.Fatalln("Can not parse config:", err) } + if ctx.Self == nil { + log.Fatalln("Config lacks private keys") + } ctx.Quiet = *quiet ctx.Debug = *debug diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-mail/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-mail/main.go index 223aea5..c09fbe2 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-mail/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-mail/main.go @@ -75,6 +75,9 @@ func main() { if err != nil { log.Fatalln("Can not parse config:", err) } + if ctx.Self == nil { + log.Fatalln("Config lacks private keys") + } ctx.Quiet = *quiet ctx.Debug = *debug diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-mincfg/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-mincfg/main.go new file mode 100644 index 0000000..5b5fdf5 --- /dev/null +++ b/src/cypherpunks.ru/nncp/cmd/nncp-mincfg/main.go @@ -0,0 +1,89 @@ +/* +NNCP -- Node to Node copy, utilities for store-and-forward data exchange +Copyright (C) 2016-2017 Sergey Matveev + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +// Stripped NNCP configuration file. +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "log" + "os" + + "cypherpunks.ru/nncp" + "gopkg.in/yaml.v2" +) + +func usage() { + fmt.Fprintf(os.Stderr, nncp.UsageHeader()) + fmt.Fprintln(os.Stderr, "nncp-mincfg -- print stripped configuration\n") + fmt.Fprintf(os.Stderr, "Usage: %s [options]\nOptions:\n", os.Args[0]) + flag.PrintDefaults() +} + +func main() { + var ( + cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file") + version = flag.Bool("version", false, "Print version information") + warranty = flag.Bool("warranty", false, "Print warranty information") + ) + flag.Usage = usage + flag.Parse() + if *warranty { + fmt.Println(nncp.Warranty) + return + } + if *version { + fmt.Println(nncp.VersionGet()) + return + } + + cfgRaw, err := ioutil.ReadFile(nncp.CfgPathFromEnv(cfgPath)) + if err != nil { + log.Fatalln("Can not read config:", err) + } + ctx, err := nncp.CfgParse(cfgRaw) + if err != nil { + log.Fatalln("Can not parse config:", err) + } + + cfg := nncp.CfgYAML{ + Spool: ctx.Spool, + Log: ctx.LogPath, + Neigh: make(map[string]nncp.NodeYAML), + } + for _, node := range ctx.Neigh { + var noisePub *string + if node.NoisePub != nil { + np := nncp.ToBase32(node.NoisePub[:]) + noisePub = &np + } + cfg.Neigh[node.Name] = nncp.NodeYAML{ + Id: node.Id.String(), + ExchPub: nncp.ToBase32(node.ExchPub[:]), + SignPub: nncp.ToBase32(node.SignPub[:]), + NoisePub: noisePub, + } + } + raw, err := yaml.Marshal(&cfg) + if err != nil { + panic(err) + } + fmt.Print(string(raw)) +} diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-newnode/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-newnode/main.go index b32a32d..080160b 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-newnode/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-newnode/main.go @@ -55,7 +55,7 @@ func main() { } noisePub := nncp.ToBase32(nodeOur.NoisePub[:]) cfg := nncp.CfgYAML{ - Self: nncp.NodeOurYAML{ + Self: &nncp.NodeOurYAML{ Id: nodeOur.Id.String(), ExchPub: nncp.ToBase32(nodeOur.ExchPub[:]), ExchPrv: nncp.ToBase32(nodeOur.ExchPrv[:]), diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-pkt/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-pkt/main.go index 82bcffb..93759b6 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-pkt/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-pkt/main.go @@ -122,6 +122,9 @@ func main() { if err != nil { log.Fatalln("Can not parse config:", err) } + if ctx.Self == nil { + log.Fatalln("Config lacks private keys") + } bufW := bufio.NewWriter(os.Stdout) if _, _, err = nncp.PktEncRead( ctx.Self, diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go index 1106695..71a15ee 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go @@ -72,6 +72,9 @@ func main() { if err != nil { log.Fatalln("Can not parse config:", err) } + if ctx.Self == nil { + log.Fatalln("Config lacks private keys") + } ctx.Quiet = *quiet ctx.Debug = *debug diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go index 9c2b5e2..b11fc5d 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go @@ -96,7 +96,7 @@ func main() { } } - selfPath := filepath.Join(flag.Arg(0), ctx.Self.Id.String()) + selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String()) isBad := false var dir *os.File var fis []os.FileInfo @@ -258,7 +258,7 @@ Tx: continue } } - dstPath := filepath.Join(nodePath, ctx.Self.Id.String()) + dstPath := filepath.Join(nodePath, ctx.SelfId.String()) sds["dir"] = dstPath _, err = os.Stat(dstPath) if err != nil { diff --git a/src/cypherpunks.ru/nncp/ctx.go b/src/cypherpunks.ru/nncp/ctx.go index c3dc144..21b3d6f 100644 --- a/src/cypherpunks.ru/nncp/ctx.go +++ b/src/cypherpunks.ru/nncp/ctx.go @@ -25,9 +25,10 @@ import ( ) type Ctx struct { - Self *NodeOur - Neigh map[NodeId]*Node - Alias map[string]*NodeId + Self *NodeOur + SelfId *NodeId + Neigh map[NodeId]*Node + Alias map[string]*NodeId Spool string LogPath string diff --git a/src/cypherpunks.ru/nncp/toss.go b/src/cypherpunks.ru/nncp/toss.go index 11e8591..9d17053 100644 --- a/src/cypherpunks.ru/nncp/toss.go +++ b/src/cypherpunks.ru/nncp/toss.go @@ -220,7 +220,7 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) bool { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove") isBad = true } - sendmail := ctx.Neigh[*ctx.Self.Id].Sendmail + sendmail := ctx.Neigh[*ctx.SelfId].Sendmail if ctx.NotifyFile != nil { cmd := exec.Command( sendmail[0], @@ -267,7 +267,7 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) bool { isBad = true } if ctx.NotifyFreq != nil { - sendmail := ctx.Neigh[*ctx.Self.Id].Sendmail + sendmail := ctx.Neigh[*ctx.SelfId].Sendmail cmd := exec.Command( sendmail[0], append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)..., diff --git a/src/cypherpunks.ru/nncp/toss_test.go b/src/cypherpunks.ru/nncp/toss_test.go index f0038d8..c40c9d8 100644 --- a/src/cypherpunks.ru/nncp/toss_test.go +++ b/src/cypherpunks.ru/nncp/toss_test.go @@ -68,6 +68,7 @@ func TestTossEmail(t *testing.T) { ctx := Ctx{ Spool: spool, Self: nodeOur, + SelfId: nodeOur.Id, Neigh: make(map[NodeId]*Node), Alias: make(map[string]*NodeId), LogPath: filepath.Join(spool, "log.log"), @@ -162,6 +163,7 @@ func TestTossFile(t *testing.T) { ctx := Ctx{ Spool: spool, Self: nodeOur, + SelfId: nodeOur.Id, Neigh: make(map[NodeId]*Node), Alias: make(map[string]*NodeId), LogPath: filepath.Join(spool, "log.log"), @@ -230,6 +232,7 @@ func TestTossFileSameName(t *testing.T) { ctx := Ctx{ Spool: spool, Self: nodeOur, + SelfId: nodeOur.Id, Neigh: make(map[NodeId]*Node), Alias: make(map[string]*NodeId), LogPath: filepath.Join(spool, "log.log"), @@ -298,6 +301,7 @@ func TestTossFreq(t *testing.T) { ctx := Ctx{ Spool: spool, Self: nodeOur, + SelfId: nodeOur.Id, Neigh: make(map[NodeId]*Node), Alias: make(map[string]*NodeId), LogPath: filepath.Join(spool, "log.log"), @@ -396,6 +400,7 @@ func TestTossTrns(t *testing.T) { ctx := Ctx{ Spool: spool, Self: nodeOur, + SelfId: nodeOur.Id, Neigh: make(map[NodeId]*Node), Alias: make(map[string]*NodeId), LogPath: filepath.Join(spool, "log.log"), diff --git a/src/cypherpunks.ru/nncp/tx_test.go b/src/cypherpunks.ru/nncp/tx_test.go index c46f057..ad721bc 100644 --- a/src/cypherpunks.ru/nncp/tx_test.go +++ b/src/cypherpunks.ru/nncp/tx_test.go @@ -58,6 +58,7 @@ func TestTx(t *testing.T) { LogPath: path.Join(spool, "log.log"), Debug: true, Self: nodeOur, + SelfId: nodeOur.Id, Neigh: make(map[NodeId]*Node, hops), Alias: make(map[string]*NodeId), } -- 2.48.1