From: Sergey Matveev Date: Thu, 23 Jan 2020 11:00:37 +0000 (+0300) Subject: Merge branch 'develop' X-Git-Tag: v5.3.3^0 X-Git-Url: http://www.git.cypherpunks.ru/?a=commitdiff_plain;h=c3abbbda3f7dc6c03702acbd534cbf196283eba8;hp=e62f2727d30cd7166d88447c234dd1f3586f11e0;p=nncp.git Merge branch 'develop' --- diff --git a/VERSION b/VERSION index 84197c8..74664af 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -5.3.2 +5.3.3 diff --git a/doc/download.texi b/doc/download.texi index 42b779d..8cb558f 100644 --- a/doc/download.texi +++ b/doc/download.texi @@ -24,6 +24,10 @@ Tarballs include all necessary required libraries: @multitable {XXXXX} {XXXX-XX-XX} {XXXX KiB} {link sign} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} @headitem Version @tab Date @tab Size @tab Tarball @tab SHA256 checksum +@item @ref{Release 5.3.2, 5.3.2} @tab 2019-12-28 @tab 1118 KiB +@tab @url{download/nncp-5.3.2.tar.xz, link} @url{download/nncp-5.3.2.tar.xz.sig, sign} +@tab @code{6E2D1B3C CA0DD462 A6F5F8DE 5CB8DE15 C3D33C74 238A2C52 373C7BD6 A126A834} + @item @ref{Release 5.3.1, 5.3.1} @tab 2019-12-25 @tab 1117 KiB @tab @url{download/nncp-5.3.1.tar.xz, link} @url{download/nncp-5.3.1.tar.xz.sig, sign} @tab @code{23A52819 F0395A6A E05E4176 017DCA3C 4A20A023 EEADA6A3 3168E58D BEE34A5B} diff --git a/doc/install.texi b/doc/install.texi index 55d1499..89c15d5 100644 --- a/doc/install.texi +++ b/doc/install.texi @@ -1,7 +1,7 @@ @node Installation @unnumbered Installation -@set VERSION 5.3.2 +@set VERSION 5.3.3 Possibly NNCP package already exists for your distribution: diff --git a/doc/news.ru.texi b/doc/news.ru.texi index 791f14e..5abadc4 100644 --- a/doc/news.ru.texi +++ b/doc/news.ru.texi @@ -1,6 +1,18 @@ @node Новости @section Новости +@node Релиз 5.3.3 +@subsection Релиз 5.3.3 +@itemize + +@item +Больше различных проверок в коде на ошибки. + +@item +Обновлены зависимые библиотеки. + +@end itemize + @node Релиз 5.3.2 @subsection Релиз 5.3.2 @itemize diff --git a/doc/news.texi b/doc/news.texi index 671f6e3..244331c 100644 --- a/doc/news.texi +++ b/doc/news.texi @@ -3,6 +3,18 @@ See also this page @ref{Новости, on russian}. +@node Release 5.3.3 +@section Release 5.3.3 +@itemize + +@item +More various error checks. + +@item +Updated dependencies. + +@end itemize + @node Release 5.3.2 @section Release 5.3.2 @itemize diff --git a/ports/nncp/Makefile b/ports/nncp/Makefile index 3efb3ea..527eb9e 100644 --- a/ports/nncp/Makefile +++ b/ports/nncp/Makefile @@ -1,7 +1,7 @@ # $FreeBSD: $ PORTNAME= nncp -DISTVERSION= 5.3.2 +DISTVERSION= 5.3.3 CATEGORIES= net MASTER_SITES= http://www.nncpgo.org/download/ diff --git a/src/base32.go b/src/base32.go deleted file mode 100644 index 0f7db5b..0000000 --- a/src/base32.go +++ /dev/null @@ -1,23 +0,0 @@ -package nncp - -import ( - "encoding/base32" - "strings" -) - -func ToBase32(data []byte) string { - return strings.TrimRight(base32.StdEncoding.EncodeToString(data), "=") -} - -func FromBase32(data string) ([]byte, error) { - padSize := len(data) % 8 - if padSize != 0 { - padSize = 8 - padSize - pad := make([]byte, 0, padSize) - for i := 0; i < padSize; i++ { - pad = append(pad, '=') - } - data += string(pad) - } - return base32.StdEncoding.DecodeString(data) -} diff --git a/src/call.go b/src/call.go index 3395498..65d7d5f 100644 --- a/src/call.go +++ b/src/call.go @@ -84,11 +84,11 @@ func (ctx *Ctx) CallNode( "txspeed": state.TxSpeed, }, "") isGood = true - conn.Close() + conn.Close() // #nosec G104 break } else { ctx.LogE("call-start", sds, err, "") - conn.Close() + conn.Close() // #nosec G104 } } return diff --git a/src/cfg.go b/src/cfg.go index f003dcb..df26f1e 100644 --- a/src/cfg.go +++ b/src/cfg.go @@ -123,7 +123,7 @@ func NewNode(name string, cfg NodeJSON) (*Node, error) { return nil, err } - exchPub, err := FromBase32(cfg.ExchPub) + exchPub, err := Base32Codec.DecodeString(cfg.ExchPub) if err != nil { return nil, err } @@ -131,7 +131,7 @@ func NewNode(name string, cfg NodeJSON) (*Node, error) { return nil, errors.New("Invalid exchPub size") } - signPub, err := FromBase32(cfg.SignPub) + signPub, err := Base32Codec.DecodeString(cfg.SignPub) if err != nil { return nil, err } @@ -141,7 +141,7 @@ func NewNode(name string, cfg NodeJSON) (*Node, error) { var noisePub []byte if cfg.NoisePub != nil { - noisePub, err = FromBase32(*cfg.NoisePub) + noisePub, err = Base32Codec.DecodeString(*cfg.NoisePub) if err != nil { return nil, err } @@ -309,7 +309,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) { return nil, err } - exchPub, err := FromBase32(cfg.ExchPub) + exchPub, err := Base32Codec.DecodeString(cfg.ExchPub) if err != nil { return nil, err } @@ -317,7 +317,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) { return nil, errors.New("Invalid exchPub size") } - exchPrv, err := FromBase32(cfg.ExchPrv) + exchPrv, err := Base32Codec.DecodeString(cfg.ExchPrv) if err != nil { return nil, err } @@ -325,7 +325,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) { return nil, errors.New("Invalid exchPrv size") } - signPub, err := FromBase32(cfg.SignPub) + signPub, err := Base32Codec.DecodeString(cfg.SignPub) if err != nil { return nil, err } @@ -333,7 +333,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) { return nil, errors.New("Invalid signPub size") } - signPrv, err := FromBase32(cfg.SignPrv) + signPrv, err := Base32Codec.DecodeString(cfg.SignPrv) if err != nil { return nil, err } @@ -341,7 +341,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) { return nil, errors.New("Invalid signPrv size") } - noisePub, err := FromBase32(cfg.NoisePub) + noisePub, err := Base32Codec.DecodeString(cfg.NoisePub) if err != nil { return nil, err } @@ -349,7 +349,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) { return nil, errors.New("Invalid noisePub size") } - noisePrv, err := FromBase32(cfg.NoisePrv) + noisePrv, err := Base32Codec.DecodeString(cfg.NoisePrv) if err != nil { return nil, err } @@ -376,12 +376,12 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) { func CfgParse(data []byte) (*Ctx, error) { var err error if bytes.Compare(data[:8], MagicNNCPBv3[:]) == 0 { - os.Stderr.WriteString("Passphrase:") + os.Stderr.WriteString("Passphrase:") // #nosec G104 password, err := terminal.ReadPassword(0) if err != nil { log.Fatalln(err) } - os.Stderr.WriteString("\n") + os.Stderr.WriteString("\n") // #nosec G104 data, err = DeEBlob(data, password) if err != nil { return nil, err diff --git a/src/check.go b/src/check.go index ae5b80f..824f0c7 100644 --- a/src/check.go +++ b/src/check.go @@ -44,11 +44,11 @@ func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool { sds := SDS{ "xx": string(xx), "node": nodeId, - "pkt": ToBase32(job.HshValue[:]), + "pkt": Base32Codec.EncodeToString(job.HshValue[:]), "fullsize": job.Size, } gut, err := Check(job.Fd, job.HshValue[:], sds, ctx.ShowPrgrs) - job.Fd.Close() + job.Fd.Close() // #nosec G104 if err != nil { ctx.LogE("check", sds, err, "") return true diff --git a/src/cmd/nncp-bundle/main.go b/src/cmd/nncp-bundle/main.go index 7685428..1cfdd45 100644 --- a/src/cmd/nncp-bundle/main.go +++ b/src/cmd/nncp-bundle/main.go @@ -127,7 +127,7 @@ func main() { sds["pkt"] = pktName if job.PktEnc.Nice > nice { ctx.LogD("nncp-bundle", sds, "too nice") - job.Fd.Close() + job.Fd.Close() // #nosec G104 continue } if err = tarWr.WriteHeader(&tar.Header{ @@ -155,14 +155,14 @@ func main() { if _, err = nncp.CopyProgressed( tarWr, job.Fd, "Tx", nncp.SdsAdd(sds, nncp.SDS{ - "pkt": nncp.ToBase32(job.HshValue[:]), + "pkt": nncp.Base32Codec.EncodeToString(job.HshValue[:]), "fullsize": job.Size, }), ctx.ShowPrgrs, ); err != nil { log.Fatalln("Error during copying to tar:", err) } - job.Fd.Close() + job.Fd.Close() // #nosec G104 if err = tarWr.Flush(); err != nil { log.Fatalln("Error during tar flushing:", err) } @@ -182,33 +182,27 @@ func main() { } } else { bufStdin := bufio.NewReaderSize(os.Stdin, CopyBufSize*2) - var peeked []byte - var prefixIdx int - var tarR *tar.Reader - var entry *tar.Header - var exists bool pktEncBuf := make([]byte, nncp.PktEncOverhead) var pktEnc *nncp.PktEnc - var pktName string - var selfPath string - var dstPath string for { - peeked, err = bufStdin.Peek(CopyBufSize) + peeked, err := bufStdin.Peek(CopyBufSize) if err != nil && err != io.EOF { log.Fatalln("Error during reading:", err) } - prefixIdx = bytes.Index(peeked, []byte(nncp.NNCPBundlePrefix)) + prefixIdx := bytes.Index(peeked, []byte(nncp.NNCPBundlePrefix)) if prefixIdx == -1 { if err == io.EOF { break } - bufStdin.Discard(bufStdin.Buffered() - (len(nncp.NNCPBundlePrefix) - 1)) + bufStdin.Discard(bufStdin.Buffered() - (len(nncp.NNCPBundlePrefix) - 1)) // #nosec G104 continue } - bufStdin.Discard(prefixIdx) - tarR = tar.NewReader(bufStdin) + if _, err = bufStdin.Discard(prefixIdx); err != nil { + panic(err) + } + tarR := tar.NewReader(bufStdin) sds["xx"] = string(nncp.TRx) - entry, err = tarR.Next() + entry, err := tarR.Next() if err != nil { if err != io.EOF { ctx.LogD( @@ -243,8 +237,8 @@ func main() { ctx.LogE("nncp-bundle", sds, errors.New("not enough spool space"), "") continue } - pktName = filepath.Base(entry.Name) - if _, err = nncp.FromBase32(pktName); err != nil { + pktName := filepath.Base(entry.Name) + if _, err = nncp.Base32Codec.DecodeString(pktName); err != nil { ctx.LogD("nncp-bundle", nncp.SdsAdd(sds, nncp.SDS{"err": "bad packet name"}), "") continue } @@ -266,16 +260,16 @@ func main() { } if *pktEnc.Sender == *ctx.SelfId && *doDelete { if len(nodeIds) > 0 { - if _, exists = nodeIds[*pktEnc.Recipient]; !exists { + if _, exists := nodeIds[*pktEnc.Recipient]; !exists { ctx.LogD("nncp-bundle", sds, "Recipient is not requested") continue } } - nodeId32 := nncp.ToBase32(pktEnc.Recipient[:]) + nodeId32 := nncp.Base32Codec.EncodeToString(pktEnc.Recipient[:]) sds["xx"] = string(nncp.TTx) sds["node"] = nodeId32 sds["pkt"] = pktName - dstPath = filepath.Join( + dstPath := filepath.Join( ctx.Spool, nodeId32, string(nncp.TTx), @@ -299,10 +293,10 @@ func main() { ); err != nil { log.Fatalln("Error during copying:", err) } - if nncp.ToBase32(hsh.Sum(nil)) == pktName { + if nncp.Base32Codec.EncodeToString(hsh.Sum(nil)) == pktName { ctx.LogI("nncp-bundle", sds, "removed") if !*dryRun { - os.Remove(dstPath) + os.Remove(dstPath) // #nosec G104 } } else { ctx.LogE("nncp-bundle", sds, errors.New("bad checksum"), "") @@ -314,16 +308,16 @@ func main() { continue } if len(nodeIds) > 0 { - if _, exists = nodeIds[*pktEnc.Sender]; !exists { + if _, exists := nodeIds[*pktEnc.Sender]; !exists { ctx.LogD("nncp-bundle", sds, "Sender is not requested") continue } } - sds["node"] = nncp.ToBase32(pktEnc.Recipient[:]) + sds["node"] = nncp.Base32Codec.EncodeToString(pktEnc.Recipient[:]) sds["pkt"] = pktName sds["fullsize"] = entry.Size - selfPath = filepath.Join(ctx.Spool, ctx.SelfId.String(), string(nncp.TRx)) - dstPath = filepath.Join(selfPath, pktName) + selfPath := filepath.Join(ctx.Spool, ctx.SelfId.String(), string(nncp.TRx)) + dstPath := filepath.Join(selfPath, pktName) if _, err = os.Stat(dstPath); err == nil || !os.IsNotExist(err) { ctx.LogD("nncp-bundle", sds, "Packet already exists") continue @@ -344,7 +338,7 @@ func main() { if _, err = nncp.CopyProgressed(hsh, tarR, "check", sds, ctx.ShowPrgrs); err != nil { log.Fatalln("Error during copying:", err) } - if nncp.ToBase32(hsh.Sum(nil)) != pktName { + if nncp.Base32Codec.EncodeToString(hsh.Sum(nil)) != pktName { ctx.LogE("nncp-bundle", sds, errors.New("bad checksum"), "") continue } @@ -362,7 +356,7 @@ func main() { if err = tmp.W.Flush(); err != nil { log.Fatalln("Error during flusing:", err) } - if nncp.ToBase32(tmp.Hsh.Sum(nil)) == pktName { + if nncp.Base32Codec.EncodeToString(tmp.Hsh.Sum(nil)) == pktName { if err = tmp.Commit(selfPath); err != nil { log.Fatalln("Error during commiting:", err) } @@ -395,7 +389,9 @@ func main() { if err = tmp.Sync(); err != nil { log.Fatalln("Error during syncing:", err) } - tmp.Close() + if err = tmp.Close(); err != nil { + log.Fatalln("Error during closing:", err) + } if err = os.MkdirAll(selfPath, os.FileMode(0777)); err != nil { log.Fatalln("Error during mkdir:", err) } diff --git a/src/cmd/nncp-call/main.go b/src/cmd/nncp-call/main.go index ae6bffe..634c003 100644 --- a/src/cmd/nncp-call/main.go +++ b/src/cmd/nncp-call/main.go @@ -142,7 +142,7 @@ func main() { splitted = strings.Split(*onlyPktsRaw, ",") onlyPkts = make(map[[32]byte]bool, len(splitted)) for _, pktIdRaw := range splitted { - pktId, err := nncp.FromBase32(pktIdRaw) + pktId, err := nncp.Base32Codec.DecodeString(pktIdRaw) if err != nil { log.Fatalln("Invalid packet specified: ", err) } diff --git a/src/cmd/nncp-cfgenc/main.go b/src/cmd/nncp-cfgenc/main.go index 7cedca0..036a4c8 100644 --- a/src/cmd/nncp-cfgenc/main.go +++ b/src/cmd/nncp-cfgenc/main.go @@ -86,40 +86,42 @@ func main() { fmt.Printf("Number of rounds: %d\n", eblob.TCost) fmt.Printf("Number of parallel jobs: %d\n", eblob.PCost) fmt.Printf("Blob size: %d\n", len(eblob.Blob)) - os.Exit(0) + return + } + + os.Stderr.WriteString("Passphrase:") // #nosec G104 + password, err := terminal.ReadPassword(0) + if err != nil { + log.Fatalln(err) } + os.Stderr.WriteString("\n") // #nosec G104 + if *decrypt { - os.Stderr.WriteString("Passphrase:") - password, err := terminal.ReadPassword(0) - if err != nil { - log.Fatalln(err) - } - os.Stderr.WriteString("\n") cfgRaw, err := nncp.DeEBlob(data, password) if err != nil { log.Fatalln(err) } - os.Stdout.Write(cfgRaw) - } else { - os.Stderr.WriteString("Passphrase:") - password1, err := terminal.ReadPassword(0) - if err != nil { - log.Fatalln(err) - } - os.Stderr.WriteString("\n") - os.Stderr.WriteString("Repeat passphrase:") - password2, err := terminal.ReadPassword(0) - if err != nil { - log.Fatalln(err) - } - os.Stderr.WriteString("\n") - if bytes.Compare(password1, password2) != 0 { - log.Fatalln(errors.New("Passphrases do not match")) - } - eblob, err := nncp.NewEBlob(*sOpt, *tOpt, *pOpt, password1, data) - if err != nil { - log.Fatalln(err) - } - os.Stdout.Write(eblob) + os.Stdout.Write(cfgRaw) // #nosec G104 + return + } + + password1, err := terminal.ReadPassword(0) + if err != nil { + log.Fatalln(err) + } + os.Stderr.WriteString("\n") // #nosec G104 + os.Stderr.WriteString("Repeat passphrase:") // #nosec G104 + password2, err := terminal.ReadPassword(0) + if err != nil { + log.Fatalln(err) + } + os.Stderr.WriteString("\n") // #nosec G104 + if bytes.Compare(password1, password2) != 0 { + log.Fatalln(errors.New("Passphrases do not match")) + } + eblob, err := nncp.NewEBlob(*sOpt, *tOpt, *pOpt, password1, data) + if err != nil { + log.Fatalln(err) } + os.Stdout.Write(eblob) // #nosec G104 } diff --git a/src/cmd/nncp-cfgmin/main.go b/src/cmd/nncp-cfgmin/main.go index b8eae7e..20bd14c 100644 --- a/src/cmd/nncp-cfgmin/main.go +++ b/src/cmd/nncp-cfgmin/main.go @@ -65,13 +65,13 @@ func main() { for _, node := range ctx.Neigh { var noisePub *string if node.NoisePub != nil { - np := nncp.ToBase32(node.NoisePub[:]) + np := nncp.Base32Codec.EncodeToString(node.NoisePub[:]) noisePub = &np } cfg.Neigh[node.Name] = nncp.NodeJSON{ Id: node.Id.String(), - ExchPub: nncp.ToBase32(node.ExchPub[:]), - SignPub: nncp.ToBase32(node.SignPub[:]), + ExchPub: nncp.Base32Codec.EncodeToString(node.ExchPub[:]), + SignPub: nncp.Base32Codec.EncodeToString(node.SignPub[:]), NoisePub: noisePub, } } diff --git a/src/cmd/nncp-cfgnew/main.go b/src/cmd/nncp-cfgnew/main.go index ff63826..8dad818 100644 --- a/src/cmd/nncp-cfgnew/main.go +++ b/src/cmd/nncp-cfgnew/main.go @@ -82,16 +82,16 @@ func main() { nncp.DefaultSpoolPath, nncp.DefaultLogPath, nodeOur.Id.String(), - nncp.ToBase32(nodeOur.ExchPub[:]), - nncp.ToBase32(nodeOur.ExchPrv[:]), - nncp.ToBase32(nodeOur.SignPub[:]), - nncp.ToBase32(nodeOur.SignPrv[:]), - nncp.ToBase32(nodeOur.NoisePrv[:]), - nncp.ToBase32(nodeOur.NoisePub[:]), + nncp.Base32Codec.EncodeToString(nodeOur.ExchPub[:]), + nncp.Base32Codec.EncodeToString(nodeOur.ExchPrv[:]), + nncp.Base32Codec.EncodeToString(nodeOur.SignPub[:]), + nncp.Base32Codec.EncodeToString(nodeOur.SignPrv[:]), + nncp.Base32Codec.EncodeToString(nodeOur.NoisePrv[:]), + nncp.Base32Codec.EncodeToString(nodeOur.NoisePub[:]), nodeOur.Id.String(), - nncp.ToBase32(nodeOur.ExchPub[:]), - nncp.ToBase32(nodeOur.SignPub[:]), - nncp.ToBase32(nodeOur.NoisePub[:]), + nncp.Base32Codec.EncodeToString(nodeOur.ExchPub[:]), + nncp.Base32Codec.EncodeToString(nodeOur.SignPub[:]), + nncp.Base32Codec.EncodeToString(nodeOur.NoisePub[:]), nncp.DefaultSendmailPath, ) } else { @@ -218,16 +218,16 @@ func main() { nncp.DefaultSpoolPath, nncp.DefaultLogPath, nodeOur.Id.String(), - nncp.ToBase32(nodeOur.ExchPub[:]), - nncp.ToBase32(nodeOur.ExchPrv[:]), - nncp.ToBase32(nodeOur.SignPub[:]), - nncp.ToBase32(nodeOur.SignPrv[:]), - nncp.ToBase32(nodeOur.NoisePrv[:]), - nncp.ToBase32(nodeOur.NoisePub[:]), + nncp.Base32Codec.EncodeToString(nodeOur.ExchPub[:]), + nncp.Base32Codec.EncodeToString(nodeOur.ExchPrv[:]), + nncp.Base32Codec.EncodeToString(nodeOur.SignPub[:]), + nncp.Base32Codec.EncodeToString(nodeOur.SignPrv[:]), + nncp.Base32Codec.EncodeToString(nodeOur.NoisePrv[:]), + nncp.Base32Codec.EncodeToString(nodeOur.NoisePub[:]), nodeOur.Id.String(), - nncp.ToBase32(nodeOur.ExchPub[:]), - nncp.ToBase32(nodeOur.SignPub[:]), - nncp.ToBase32(nodeOur.NoisePub[:]), + nncp.Base32Codec.EncodeToString(nodeOur.ExchPub[:]), + nncp.Base32Codec.EncodeToString(nodeOur.SignPub[:]), + nncp.Base32Codec.EncodeToString(nodeOur.NoisePub[:]), nncp.DefaultSendmailPath, nncp.DefaultSendmailPath, ) diff --git a/src/cmd/nncp-daemon/main.go b/src/cmd/nncp-daemon/main.go index 2d52fcf..9d614f7 100644 --- a/src/cmd/nncp-daemon/main.go +++ b/src/cmd/nncp-daemon/main.go @@ -60,7 +60,7 @@ func (c InetdConn) SetWriteDeadline(t time.Time) error { func (c InetdConn) Close() error { if err := c.r.Close(); err != nil { - c.w.Close() + c.w.Close() // #nosec G104 return err } return c.w.Close() @@ -140,10 +140,10 @@ func main() { ctx.Umask() if *inetd { - os.Stderr.Close() + os.Stderr.Close() // #nosec G104 conn := &InetdConn{os.Stdin, os.Stdout} performSP(ctx, conn, nice) - conn.Close() + conn.Close() // #nosec G104 return } @@ -160,7 +160,7 @@ func main() { ctx.LogD("daemon", nncp.SDS{"addr": conn.RemoteAddr()}, "accepted") go func(conn net.Conn) { performSP(ctx, conn, nice) - conn.Close() + conn.Close() // #nosec G104 }(conn) } } diff --git a/src/cmd/nncp-pkt/main.go b/src/cmd/nncp-pkt/main.go index 3f42cd2..1264974 100644 --- a/src/cmd/nncp-pkt/main.go +++ b/src/cmd/nncp-pkt/main.go @@ -118,7 +118,7 @@ func main() { -1, )) case nncp.PktTypeTrns: - path = nncp.ToBase32(pkt.Path[:pkt.PathLen]) + path = nncp.Base32Codec.EncodeToString(pkt.Path[:pkt.PathLen]) default: path = string(pkt.Path[:pkt.PathLen]) } diff --git a/src/cmd/nncp-reass/main.go b/src/cmd/nncp-reass/main.go index f39d5ac..8e1ba0b 100644 --- a/src/cmd/nncp-reass/main.go +++ b/src/cmd/nncp-reass/main.go @@ -61,7 +61,7 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo ctx.LogE("nncp-reass", nncp.SDS{"path": path}, err, "bad meta file") return false } - fd.Close() + fd.Close() // #nosec G104 if metaPkt.Magic != nncp.MagicNNCPMv1 { ctx.LogE("nncp-reass", nncp.SDS{"path": path}, nncp.BadMagic, "") return false @@ -154,7 +154,7 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo ); err != nil { log.Fatalln(err) } - fd.Close() + fd.Close() // #nosec G104 if bytes.Compare(hsh.Sum(nil), metaPkt.Checksums[chunkNum][:]) != 0 { ctx.LogE( "nncp-reass", @@ -209,7 +209,7 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo ); err != nil { log.Fatalln(err) } - fd.Close() + fd.Close() // #nosec G104 if !keep { if err = os.Remove(chunkPath); err != nil { ctx.LogE("nncp-reass", nncp.SdsAdd(sds, nncp.SDS{"chunk": chunkNum}), err, "") @@ -224,7 +224,9 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo if err = tmp.Sync(); err != nil { log.Fatalln("Can not sync:", err) } - tmp.Close() + if err = tmp.Close(); err != nil { + log.Fatalln("Can not close:", err) + } } ctx.LogD("nncp-reass", sds, "written") if !keep { @@ -269,7 +271,7 @@ func findMetas(ctx *nncp.Ctx, dirPath string) []string { return nil } fis, err := dir.Readdir(0) - dir.Close() + dir.Close() // #nosec G104 if err != nil { ctx.LogE("nncp-reass", nncp.SDS{"path": dirPath}, err, "") return nil diff --git a/src/cmd/nncp-stat/main.go b/src/cmd/nncp-stat/main.go index 09cc63b..fa33d4c 100644 --- a/src/cmd/nncp-stat/main.go +++ b/src/cmd/nncp-stat/main.go @@ -40,7 +40,7 @@ func jobPrint(xx nncp.TRxTx, job nncp.Job) { fmt.Printf( "\t%s %s %s (nice: %s)\n", string(xx), - nncp.ToBase32(job.HshValue[:]), + nncp.Base32Codec.EncodeToString(job.HshValue[:]), humanize.IBytes(uint64(job.Size)), nncp.NicenessFmt(job.PktEnc.Nice), ) @@ -99,7 +99,7 @@ func main() { rxNums := make(map[uint8]int) rxBytes := make(map[uint8]int64) for job := range ctx.Jobs(node.Id, nncp.TRx) { - job.Fd.Close() + job.Fd.Close() // #nosec G104 if *showPkt { jobPrint(nncp.TRx, job) } @@ -109,7 +109,7 @@ func main() { txNums := make(map[uint8]int) txBytes := make(map[uint8]int64) for job := range ctx.Jobs(node.Id, nncp.TTx) { - job.Fd.Close() + job.Fd.Close() // #nosec G104 if *showPkt { jobPrint(nncp.TRx, job) } diff --git a/src/cmd/nncp-xfer/main.go b/src/cmd/nncp-xfer/main.go index 1a2351d..77cd9c6 100644 --- a/src/cmd/nncp-xfer/main.go +++ b/src/cmd/nncp-xfer/main.go @@ -128,7 +128,7 @@ func main() { goto Tx } fis, err = dir.Readdir(0) - dir.Close() + dir.Close() // #nosec G104 if err != nil { ctx.LogE("nncp-xfer", sds, err, "read") isBad = true @@ -159,7 +159,7 @@ func main() { continue } fisInt, err := dir.Readdir(0) - dir.Close() + dir.Close() // #nosec G104 if err != nil { ctx.LogE("nncp-xfer", sds, err, "read") isBad = true @@ -186,21 +186,23 @@ func main() { _, err = xdr.Unmarshal(fd, &pktEnc) if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 { ctx.LogD("nncp-xfer", sds, "is not a packet") - fd.Close() + fd.Close() // #nosec G104 continue } if pktEnc.Nice > nice { ctx.LogD("nncp-xfer", sds, "too nice") - fd.Close() + fd.Close() // #nosec G104 continue } sds["size"] = fiInt.Size() if !ctx.IsEnoughSpace(fiInt.Size()) { ctx.LogE("nncp-xfer", sds, errors.New("is not enough space"), "") - fd.Close() + fd.Close() // #nosec G104 continue } - fd.Seek(0, 0) + if _, err = fd.Seek(0, 0); err != nil { + log.Fatalln(err) + } tmp, err := ctx.NewTmpFileWHash() if err != nil { log.Fatalln(err) @@ -209,11 +211,12 @@ func main() { go func() { _, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size()) if err == nil { - w.Close() - return + err = w.Close() + } + if err != nil { + ctx.LogE("nncp-xfer", sds, err, "copy") + w.CloseWithError(err) // #nosec G104 } - ctx.LogE("nncp-xfer", sds, err, "copy") - w.CloseWithError(err) }() if _, err = nncp.CopyProgressed( tmp.W, r, "Rx", @@ -226,7 +229,7 @@ func main() { ctx.LogE("nncp-xfer", sds, err, "copy") isBad = true } - fd.Close() + fd.Close() // #nosec G104 if isBad { tmp.Cancel() continue @@ -313,23 +316,23 @@ Tx: sds["pkt"] = pktName if job.PktEnc.Nice > nice { ctx.LogD("nncp-xfer", sds, "too nice") - job.Fd.Close() + job.Fd.Close() // #nosec G104 continue } if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) { ctx.LogD("nncp-xfer", sds, "already exists") - job.Fd.Close() + job.Fd.Close() // #nosec G104 continue } if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) { ctx.LogD("nncp-xfer", sds, "already exists") - job.Fd.Close() + job.Fd.Close() // #nosec G104 continue } tmp, err := nncp.TempFile(dstPath, "xfer") if err != nil { ctx.LogE("nncp-xfer", sds, err, "mktemp") - job.Fd.Close() + job.Fd.Close() // #nosec G104 isBad = true break } @@ -341,26 +344,28 @@ Tx: nncp.SdsAdd(sds, nncp.SDS{"fullsize": job.Size}), ctx.ShowPrgrs, ) - job.Fd.Close() + job.Fd.Close() // #nosec G104 if err != nil { ctx.LogE("nncp-xfer", sds, err, "copy") - tmp.Close() + tmp.Close() // #nosec G104 isBad = true continue } if err = bufW.Flush(); err != nil { - tmp.Close() + tmp.Close() // #nosec G104 ctx.LogE("nncp-xfer", sds, err, "flush") isBad = true continue } if err = tmp.Sync(); err != nil { - tmp.Close() + tmp.Close() // #nosec G104 ctx.LogE("nncp-xfer", sds, err, "sync") isBad = true continue } - tmp.Close() + if err = tmp.Close(); err != nil { + ctx.LogE("nncp-xfer", sds, err, "sync") + } if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil { ctx.LogE("nncp-xfer", sds, err, "rename") isBad = true @@ -371,7 +376,7 @@ Tx: isBad = true continue } - os.Remove(filepath.Join(dstPath, pktName+".part")) + os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104 delete(sds, "tmp") ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"size": copied}), "") if !*keep { diff --git a/src/ctx.go b/src/ctx.go index c1625a1..9196930 100644 --- a/src/ctx.go +++ b/src/ctx.go @@ -73,8 +73,7 @@ func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error { ctx.LogE("dir-ensure", SDS{"dir": dirPath}, err, "") return err } - fd.Close() - return nil + return fd.Close() } func CtxFromCmdline( diff --git a/src/go.mod b/src/go.mod index eca5fae..b2710f7 100644 --- a/src/go.mod +++ b/src/go.mod @@ -4,15 +4,14 @@ require ( github.com/davecgh/go-xdr v0.0.0-20161123171359-e6a2ba005892 github.com/dustin/go-humanize v1.0.0 github.com/flynn/noise v0.0.0-20180327030543-2492fe189ae6 - github.com/google/go-cmp v0.3.1 // indirect github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 github.com/hjson/hjson-go v3.0.1+incompatible - github.com/klauspost/compress v1.9.2 + github.com/klauspost/compress v1.9.8 github.com/kr/pretty v0.1.0 // indirect go.cypherpunks.ru/balloon v1.1.1 - golang.org/x/crypto v0.0.0-20191219195013-becbf705a915 - golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 - golang.org/x/sys v0.0.0-20191219235734-af0d71d358ab + golang.org/x/crypto v0.0.0-20200117160349-530e935923ad + golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa + golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) diff --git a/src/go.sum b/src/go.sum index 2a006d9..f29a40b 100644 --- a/src/go.sum +++ b/src/go.sum @@ -4,14 +4,12 @@ github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4 github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/flynn/noise v0.0.0-20180327030543-2492fe189ae6 h1:u/UEqS66A5ckRmS4yNpjmVH56sVtS/RfclBAYocb4as= github.com/flynn/noise v0.0.0-20180327030543-2492fe189ae6/go.mod h1:1i71OnUq3iUe1ma7Lr6yG6/rjvM3emb6yoL7xLFzcVQ= -github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg= -github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 h1:f0n1xnMSmBLzVfsMMvriDyA75NB/oBgILX2GcHXIQzY= github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75/go.mod h1:g2644b03hfBX9Ov0ZBDgXXens4rxSxmqFBbhvKv2yVA= github.com/hjson/hjson-go v3.0.1+incompatible h1:JwOXblcMiBbiWue7iPkoFK9oXSnW8n+qXh/0Fio6TCo= github.com/hjson/hjson-go v3.0.1+incompatible/go.mod h1:qsetwF8NlsTsOTwZTApNlTCerV+b2GjYRRcIk4JMFio= -github.com/klauspost/compress v1.9.2 h1:LfVyl+ZlLlLDeQ/d2AqfGIIH4qEDu0Ed2S5GyhCWIWY= -github.com/klauspost/compress v1.9.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= +github.com/klauspost/compress v1.9.8 h1:VMAMUUOh+gaxKTMk+zqbjsSjsIcUcL/LF4o63i82QyA= +github.com/klauspost/compress v1.9.8/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -20,16 +18,16 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= go.cypherpunks.ru/balloon v1.1.1 h1:ypHM1DRf/XuCrp9pDkTHg00CqZX/Np/APb//iHvDJTA= go.cypherpunks.ru/balloon v1.1.1/go.mod h1:k4s4ozrIrhpBjj78Z7LX8ZHxMQ+XE7DZUWl8gP2ojCo= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191219195013-becbf705a915 h1:aJ0ex187qoXrJHPo8ZasVTASQB7llQP6YeNzgDALPRk= -golang.org/x/crypto v0.0.0-20191219195013-becbf705a915/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200117160349-530e935923ad h1:Jh8cai0fqIK+f6nG0UgPW5wFk8wmiMhM3AyciDBdtQg= +golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= -golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA= +golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191219235734-af0d71d358ab h1:j8r8g0V3tVdbo274kyTmC+yEsChru2GfvdiV84wm5T8= -golang.org/x/sys v0.0.0-20191219235734-af0d71d358ab/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 h1:ywK/j/KkyTHcdyYSZNXGjMwgmDSfjglYZ3vStQ/gSCU= +golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/src/jobs.go b/src/jobs.go index 16c95bf..d7363ed 100644 --- a/src/jobs.go +++ b/src/jobs.go @@ -49,12 +49,12 @@ func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job { return } fis, err := dir.Readdir(0) - dir.Close() + dir.Close() // #nosec G104 if err != nil { return } for _, fi := range fis { - hshValue, err := FromBase32(fi.Name()) + hshValue, err := Base32Codec.DecodeString(fi.Name()) if err != nil { continue } @@ -64,10 +64,13 @@ func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job { } var pktEnc PktEnc if _, err = xdr.Unmarshal(fd, &pktEnc); err != nil || pktEnc.Magic != MagicNNCPEv4 { - fd.Close() + fd.Close() // #nosec G104 + continue + } + if _, err = fd.Seek(0, io.SeekStart); err != nil { + fd.Close() // #nosec G104 continue } - fd.Seek(0, io.SeekStart) ctx.LogD("jobs", SDS{ "xx": string(xx), "node": pktEnc.Sender, diff --git a/src/lockdir.go b/src/lockdir.go index f87e6c6..875a5bb 100644 --- a/src/lockdir.go +++ b/src/lockdir.go @@ -25,7 +25,10 @@ import ( ) func (ctx *Ctx) LockDir(nodeId *NodeId, lockCtx string) (*os.File, error) { - ctx.ensureRxDir(nodeId) + if err := ctx.ensureRxDir(nodeId); err != nil { + ctx.LogE("lockdir", SDS{}, err, "") + return nil, err + } lockPath := filepath.Join(ctx.Spool, nodeId.String(), lockCtx) + ".lock" dirLock, err := os.OpenFile( lockPath, @@ -39,7 +42,7 @@ func (ctx *Ctx) LockDir(nodeId *NodeId, lockCtx string) (*os.File, error) { err = unix.Flock(int(dirLock.Fd()), unix.LOCK_EX|unix.LOCK_NB) if err != nil { ctx.LogE("lockdir", SDS{"path": lockPath}, err, "") - dirLock.Close() + dirLock.Close() // #nosec G104 return nil, err } return dirLock, nil @@ -47,7 +50,7 @@ func (ctx *Ctx) LockDir(nodeId *NodeId, lockCtx string) (*os.File, error) { func (ctx *Ctx) UnlockDir(fd *os.File) { if fd != nil { - unix.Flock(int(fd.Fd()), unix.LOCK_UN) - fd.Close() + unix.Flock(int(fd.Fd()), unix.LOCK_UN) // #nosec G104 + fd.Close() // #nosec G104 } } diff --git a/src/log.go b/src/log.go index a5bef48..dfa2a4c 100644 --- a/src/log.go +++ b/src/log.go @@ -91,8 +91,8 @@ func (ctx *Ctx) Log(msg string) { fmt.Fprintln(os.Stderr, "Can not open log:", err) return } - fd.WriteString(msg) - fd.Close() + fd.WriteString(msg) // #nosec G104 + fd.Close() // #nosec G104 } func (ctx *Ctx) LogD(who string, sds SDS, msg string) { diff --git a/src/nncp.go b/src/nncp.go index 93e4446..22d7578 100644 --- a/src/nncp.go +++ b/src/nncp.go @@ -19,6 +19,7 @@ along with this program. If not, see . package nncp import ( + "encoding/base32" "runtime" ) @@ -38,6 +39,8 @@ along with this program. If not, see .` var ( Version string = "UNKNOWN" + + Base32Codec *base32.Encoding = base32.StdEncoding.WithPadding(base32.NoPadding) ) func VersionGet() string { diff --git a/src/node.go b/src/node.go index 7bfd0ef..999af3d 100644 --- a/src/node.go +++ b/src/node.go @@ -32,7 +32,7 @@ import ( type NodeId [blake2b.Size256]byte func (id NodeId) String() string { - return ToBase32(id[:]) + return Base32Codec.EncodeToString(id[:]) } type Node struct { @@ -112,7 +112,7 @@ func (nodeOur *NodeOur) Their() *Node { } func NodeIdFromString(raw string) (*NodeId, error) { - decoded, err := FromBase32(raw) + decoded, err := Base32Codec.DecodeString(raw) if err != nil { return nil, err } diff --git a/src/pipe.go b/src/pipe.go index 1b50573..a117bc8 100644 --- a/src/pipe.go +++ b/src/pipe.go @@ -65,9 +65,11 @@ func (c PipeConn) SetWriteDeadline(t time.Time) error { } func (c PipeConn) Close() (err error) { - c.r.Close() + err = c.r.Close() err = c.w.Close() go c.cmd.Wait() - time.AfterFunc(time.Duration(10*time.Second), func() { c.cmd.Process.Kill() }) + time.AfterFunc(time.Duration(10*time.Second), func() { + c.cmd.Process.Kill() // #nosec G104 + }) return } diff --git a/src/sp.go b/src/sp.go index 37cf265..94b37ff 100644 --- a/src/sp.go +++ b/src/sp.go @@ -290,7 +290,7 @@ func (ctx *Ctx) infosOur(nodeId *NodeId, nice uint8, seen *map[[32]byte]uint8) [ var infos []*SPInfo var totalSize int64 for job := range ctx.Jobs(nodeId, TTx) { - job.Fd.Close() + job.Fd.Close() // #nosec G104 if job.PktEnc.Nice > nice { continue } @@ -311,7 +311,7 @@ func (ctx *Ctx) infosOur(nodeId *NodeId, nice uint8, seen *map[[32]byte]uint8) [ payloads = append(payloads, MarshalSP(SPTypeInfo, info)) ctx.LogD("sp-info-our", SDS{ "node": nodeId, - "name": ToBase32(info.Hash[:]), + "name": Base32Codec.EncodeToString(info.Hash[:]), "size": info.Size, }, "") } @@ -392,14 +392,14 @@ func (state *SPState) StartI(conn ConnDeadlined) error { } sds := SDS{"node": nodeId, "nice": int(state.Nice)} state.Ctx.LogD("sp-start", sds, "sending first message") - conn.SetWriteDeadline(time.Now().Add(DefaultDeadline)) + conn.SetWriteDeadline(time.Now().Add(DefaultDeadline)) // #nosec G104 if err = state.WriteSP(conn, buf, false); err != nil { state.Ctx.LogE("sp-start", sds, err, "") state.dirUnlock() return err } state.Ctx.LogD("sp-start", sds, "waiting for first message") - conn.SetReadDeadline(time.Now().Add(DefaultDeadline)) + conn.SetReadDeadline(time.Now().Add(DefaultDeadline)) // #nosec G104 if buf, err = state.ReadSP(conn); err != nil { state.Ctx.LogE("sp-start", sds, err, "") state.dirUnlock() @@ -446,7 +446,7 @@ func (state *SPState) StartR(conn ConnDeadlined) error { var buf []byte var payload []byte state.Ctx.LogD("sp-start", SDS{"nice": int(state.Nice)}, "waiting for first message") - conn.SetReadDeadline(time.Now().Add(DefaultDeadline)) + conn.SetReadDeadline(time.Now().Add(DefaultDeadline)) // #nosec G104 if buf, err = state.ReadSP(conn); err != nil { state.Ctx.LogE("sp-start", SDS{}, err, "") return err @@ -464,7 +464,7 @@ func (state *SPState) StartR(conn ConnDeadlined) error { } } if node == nil { - peerId := ToBase32(state.hs.PeerStatic()) + peerId := Base32Codec.EncodeToString(state.hs.PeerStatic()) state.Ctx.LogE("sp-start", SDS{"peer": peerId}, errors.New("unknown"), "") return errors.New("Unknown peer: " + peerId) } @@ -475,7 +475,7 @@ func (state *SPState) StartR(conn ConnDeadlined) error { state.maxOnlineTime = node.MaxOnlineTime sds := SDS{"node": node.Id, "nice": int(state.Nice)} - if state.Ctx.ensureRxDir(node.Id); err != nil { + if err = state.Ctx.ensureRxDir(node.Id); err != nil { return err } var rxLock *os.File @@ -514,7 +514,7 @@ func (state *SPState) StartR(conn ConnDeadlined) error { state.dirUnlock() return err } - conn.SetWriteDeadline(time.Now().Add(DefaultDeadline)) + conn.SetWriteDeadline(time.Now().Add(DefaultDeadline)) // #nosec G104 if err = state.WriteSP(conn, buf, false); err != nil { state.Ctx.LogE("sp-start", sds, err, "") state.dirUnlock() @@ -597,7 +597,7 @@ func (state *SPState) StartWorkers( (state.maxOnlineTime > 0 && state.mustFinishAt.Before(now)) || (now.Sub(state.RxLastSeen) >= 2*PingTimeout) { state.SetDead() - conn.Close() + conn.Close() // #nosec G104 } case now := <-pingTicker.C: if now.After(state.TxLastSeen.Add(PingTimeout)) { @@ -677,7 +677,7 @@ func (state *SPState) StartWorkers( } sdsp := SdsAdd(sds, SDS{ "xx": string(TTx), - "pkt": ToBase32(freq.Hash[:]), + "pkt": Base32Codec.EncodeToString(freq.Hash[:]), "size": int64(freq.Offset), }) state.Ctx.LogD("sp-file", sdsp, "queueing") @@ -685,7 +685,7 @@ func (state *SPState) StartWorkers( state.Ctx.Spool, state.Node.Id.String(), string(TTx), - ToBase32(freq.Hash[:]), + Base32Codec.EncodeToString(freq.Hash[:]), )) if err != nil { state.Ctx.LogE("sp-file", sdsp, err, "") @@ -713,7 +713,7 @@ func (state *SPState) StartWorkers( buf = buf[:n] state.Ctx.LogD("sp-file", SdsAdd(sdsp, SDS{"size": n}), "read") } - fd.Close() + fd.Close() // #nosec G104 payload = MarshalSP(SPTypeFile, SPFile{ Hash: freq.Hash, Offset: freq.Offset, @@ -743,7 +743,7 @@ func (state *SPState) StartWorkers( state.Unlock() } state.Ctx.LogD("sp-xmit", SdsAdd(sds, SDS{"size": len(payload)}), "sending") - conn.SetWriteDeadline(time.Now().Add(DefaultDeadline)) + conn.SetWriteDeadline(time.Now().Add(DefaultDeadline)) // #nosec G104 if err := state.WriteSP(conn, state.csOur.Encrypt(nil, nil, payload), ping); err != nil { state.Ctx.LogE("sp-xmit", sds, err, "") return @@ -759,7 +759,7 @@ func (state *SPState) StartWorkers( break } state.Ctx.LogD("sp-recv", sds, "waiting for payload") - conn.SetReadDeadline(time.Now().Add(DefaultDeadline)) + conn.SetReadDeadline(time.Now().Add(DefaultDeadline)) // #nosec G104 payload, err := state.ReadSP(conn) if err != nil { if err == io.EOF { @@ -814,7 +814,7 @@ func (state *SPState) StartWorkers( state.SetDead() state.wg.Done() state.SetDead() - conn.Close() + conn.Close() // #nosec G104 }() return nil @@ -872,7 +872,7 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { return nil, err } sdsp = SdsAdd(sds, SDS{ - "pkt": ToBase32(info.Hash[:]), + "pkt": Base32Codec.EncodeToString(info.Hash[:]), "size": int64(info.Size), "nice": int(info.Nice), }) @@ -892,7 +892,7 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { state.Ctx.Spool, state.Node.Id.String(), string(TRx), - ToBase32(info.Hash[:]), + Base32Codec.EncodeToString(info.Hash[:]), ) if _, err = os.Stat(pktPath); err == nil { state.Ctx.LogI("sp-info", sdsp, "already done") @@ -937,14 +937,14 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { return nil, err } sdsp["xx"] = string(TRx) - sdsp["pkt"] = ToBase32(file.Hash[:]) + sdsp["pkt"] = Base32Codec.EncodeToString(file.Hash[:]) sdsp["size"] = len(file.Payload) dirToSync := filepath.Join( state.Ctx.Spool, state.Node.Id.String(), string(TRx), ) - filePath := filepath.Join(dirToSync, ToBase32(file.Hash[:])) + filePath := filepath.Join(dirToSync, Base32Codec.EncodeToString(file.Hash[:])) state.Ctx.LogD("sp-file", sdsp, "opening part") fd, err := os.OpenFile( filePath+PartSuffix, @@ -962,14 +962,14 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { ) if _, err = fd.Seek(int64(file.Offset), io.SeekStart); err != nil { state.Ctx.LogE("sp-file", sdsp, err, "") - fd.Close() + fd.Close() // #nosec G104 return nil, err } state.Ctx.LogD("sp-file", sdsp, "writing") _, err = fd.Write(file.Payload) if err != nil { state.Ctx.LogE("sp-file", sdsp, err, "") - fd.Close() + fd.Close() // #nosec G104 return nil, err } ourSize := int64(file.Offset + uint64(len(file.Payload))) @@ -986,7 +986,7 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { Progress("Rx", sdsp) } if fullsize != ourSize { - fd.Close() + fd.Close() // #nosec G104 continue } spWorkersGroup.Wait() @@ -994,15 +994,19 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { go func() { if err := fd.Sync(); err != nil { state.Ctx.LogE("sp-file", sdsp, err, "sync") - fd.Close() + fd.Close() // #nosec G104 return } state.wg.Add(1) defer state.wg.Done() - fd.Seek(0, io.SeekStart) + if _, err = fd.Seek(0, io.SeekStart); err != nil { + fd.Close() // #nosec G104 + state.Ctx.LogE("sp-file", sdsp, err, "") + return + } state.Ctx.LogD("sp-file", sdsp, "checking") gut, err := Check(fd, file.Hash[:], sdsp, state.Ctx.ShowPrgrs) - fd.Close() + fd.Close() // #nosec G104 if err != nil || !gut { state.Ctx.LogE("sp-file", sdsp, errors.New("checksum mismatch"), "") return @@ -1034,13 +1038,13 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { state.Ctx.LogE("sp-process", SdsAdd(sds, SDS{"type": "done"}), err, "") return nil, err } - sdsp["pkt"] = ToBase32(done.Hash[:]) + sdsp["pkt"] = Base32Codec.EncodeToString(done.Hash[:]) state.Ctx.LogD("sp-done", sdsp, "removing") err := os.Remove(filepath.Join( state.Ctx.Spool, state.Node.Id.String(), string(TTx), - ToBase32(done.Hash[:]), + Base32Codec.EncodeToString(done.Hash[:]), )) sdsp["xx"] = string(TTx) if err == nil { @@ -1056,7 +1060,7 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { state.Ctx.LogE("sp-process", sdsp, err, "") return nil, err } - sdsp["pkt"] = ToBase32(freq.Hash[:]) + sdsp["pkt"] = Base32Codec.EncodeToString(freq.Hash[:]) sdsp["offset"] = freq.Offset state.Ctx.LogD("sp-process", sdsp, "queueing") nice, exists := state.infosOurSeen[*freq.Hash] diff --git a/src/tmp.go b/src/tmp.go index 32c3fdb..62ec5bb 100644 --- a/src/tmp.go +++ b/src/tmp.go @@ -74,9 +74,9 @@ func (ctx *Ctx) NewTmpFileWHash() (*TmpFileWHash, error) { } func (tmp *TmpFileWHash) Cancel() { - tmp.Fd.Truncate(0) - tmp.Fd.Close() - os.Remove(tmp.Fd.Name()) + tmp.Fd.Truncate(0) // #nosec G104 + tmp.Fd.Close() // #nosec G104 + os.Remove(tmp.Fd.Name()) // #nosec G104 } func DirSync(dirPath string) error { @@ -86,7 +86,7 @@ func DirSync(dirPath string) error { } err = fd.Sync() if err != nil { - fd.Close() + fd.Close() // #nosec G104 return err } return fd.Close() @@ -98,15 +98,17 @@ func (tmp *TmpFileWHash) Commit(dir string) error { return err } if err = tmp.W.Flush(); err != nil { - tmp.Fd.Close() + tmp.Fd.Close() // #nosec G104 return err } if err = tmp.Fd.Sync(); err != nil { - tmp.Fd.Close() + tmp.Fd.Close() // #nosec G104 return err } - tmp.Fd.Close() - checksum := ToBase32(tmp.Hsh.Sum(nil)) + if err = tmp.Fd.Close(); err != nil { + return err + } + checksum := Base32Codec.EncodeToString(tmp.Hsh.Sum(nil)) tmp.ctx.LogD("tmp", SDS{"src": tmp.Fd.Name(), "dst": checksum}, "commit") if err = os.Rename(tmp.Fd.Name(), filepath.Join(dir, checksum)); err != nil { return err diff --git a/src/toss.go b/src/toss.go index 0bedeca..c0aa7bf 100644 --- a/src/toss.go +++ b/src/toss.go @@ -89,8 +89,7 @@ func (ctx *Ctx) Toss( continue } pipeR, pipeW := io.Pipe() - errs := make(chan error, 1) - go func(job Job) { + go func(job Job) error { pipeWB := bufio.NewWriter(pipeW) _, _, err := PktEncRead( ctx.Self, @@ -98,13 +97,16 @@ func (ctx *Ctx) Toss( bufio.NewReader(job.Fd), pipeWB, ) - errs <- err - pipeWB.Flush() - pipeW.Close() - job.Fd.Close() + job.Fd.Close() // #nosec G104 if err != nil { ctx.LogE("rx", sds, err, "decryption") + return pipeW.CloseWithError(err) } + if err = pipeWB.Flush(); err != nil { + ctx.LogE("rx", sds, err, "decryption flush") + return pipeW.CloseWithError(err) + } + return pipeW.Close() }(job) var pkt Pkt var err error @@ -180,7 +182,9 @@ func (ctx *Ctx) Toss( cmd.Stdin = newNotification(notify, fmt.Sprintf( "Exec from %s: %s", sender.Name, argsStr, ), output) - cmd.Run() + if err = cmd.Run(); err != nil { + ctx.LogE("rx", sds, err, "notify") + } } } } @@ -188,7 +192,7 @@ func (ctx *Ctx) Toss( if !dryRun { if doSeen { if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil { - fd.Close() + fd.Close() // #nosec G104 } } if err = os.Remove(job.Fd.Name()); err != nil { @@ -239,18 +243,22 @@ func (ctx *Ctx) Toss( goto Closing } if err = bufW.Flush(); err != nil { - tmp.Close() + tmp.Close() // #nosec G104 ctx.LogE("rx", sds, err, "copy") isBad = true goto Closing } if err = tmp.Sync(); err != nil { - tmp.Close() + tmp.Close() // #nosec G104 + ctx.LogE("rx", sds, err, "copy") + isBad = true + goto Closing + } + if err = tmp.Close(); err != nil { ctx.LogE("rx", sds, err, "copy") isBad = true goto Closing } - tmp.Close() dstPathOrig := filepath.Join(*incoming, dst) dstPath := dstPathOrig dstPathCtr := 0 @@ -280,7 +288,7 @@ func (ctx *Ctx) Toss( if !dryRun { if doSeen { if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil { - fd.Close() + fd.Close() // #nosec G104 } } if err = os.Remove(job.Fd.Name()); err != nil { @@ -298,7 +306,9 @@ func (ctx *Ctx) Toss( dst, humanize.IBytes(uint64(pktSize)), ), nil) - cmd.Run() + if err = cmd.Run(); err != nil { + ctx.LogE("rx", sds, err, "notify") + } } } case PktTypeFreq: @@ -347,7 +357,7 @@ func (ctx *Ctx) Toss( if !dryRun { if doSeen { if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil { - fd.Close() + fd.Close() // #nosec G104 } } if err = os.Remove(job.Fd.Name()); err != nil { @@ -362,7 +372,9 @@ func (ctx *Ctx) Toss( cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf( "Freq from %s: %s", sender.Name, src, ), nil) - cmd.Run() + if err = cmd.Run(); err != nil { + ctx.LogE("rx", sds, err, "notify") + } } } case PktTypeTrns: @@ -391,7 +403,7 @@ func (ctx *Ctx) Toss( if !dryRun { if doSeen { if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil { - fd.Close() + fd.Close() // #nosec G104 } } if err = os.Remove(job.Fd.Name()); err != nil { @@ -404,7 +416,7 @@ func (ctx *Ctx) Toss( isBad = true } Closing: - pipeR.Close() + pipeR.Close() // #nosec G104 } return isBad } diff --git a/src/toss_test.go b/src/toss_test.go index b6df326..a76615d 100644 --- a/src/toss_test.go +++ b/src/toss_test.go @@ -189,7 +189,7 @@ func TestTossFile(t *testing.T) { incomingPath := filepath.Join(spool, "incoming") for _, fileData := range files { checksum := blake2b.Sum256(fileData) - fileName := ToBase32(checksum[:]) + fileName := Base32Codec.EncodeToString(checksum[:]) src := filepath.Join(spool, fileName) if err := ioutil.WriteFile(src, fileData, os.FileMode(0600)); err != nil { panic(err) @@ -220,7 +220,7 @@ func TestTossFile(t *testing.T) { } for _, fileData := range files { checksum := blake2b.Sum256(fileData) - fileName := ToBase32(checksum[:]) + fileName := Base32Codec.EncodeToString(checksum[:]) data, err := ioutil.ReadFile(filepath.Join(incomingPath, fileName)) if err != nil { panic(err) @@ -468,7 +468,7 @@ func TestTossTrns(t *testing.T) { } checksum := blake2b.Sum256(dst.Bytes()) if err := ioutil.WriteFile( - filepath.Join(rxPath, ToBase32(checksum[:])), + filepath.Join(rxPath, Base32Codec.EncodeToString(checksum[:])), dst.Bytes(), os.FileMode(0600), ); err != nil { diff --git a/src/tx.go b/src/tx.go index 831cb6f..4c31d92 100644 --- a/src/tx.go +++ b/src/tx.go @@ -86,7 +86,7 @@ func (ctx *Ctx) Tx( "size": size, }, "wrote") errs <- PktEncWrite(ctx.Self, hops[0], pkt, nice, size, padSize, src, dst) - dst.Close() + dst.Close() // #nosec G104 }(curSize, src, pipeW) curSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+curSize) + padSize @@ -102,7 +102,7 @@ func (ctx *Ctx) Tx( "size": size, }, "trns wrote") errs <- PktEncWrite(ctx.Self, node, pkt, nice, size, 0, src, dst) - dst.Close() + dst.Close() // #nosec G104 }(hops[i], pktTrns, curSize, pipeRPrev, pipeW) curSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+curSize) } @@ -117,13 +117,13 @@ func (ctx *Ctx) Tx( for i := 0; i <= len(hops); i++ { err = <-errs if err != nil { - tmp.Fd.Close() + tmp.Fd.Close() // #nosec G104 return nil, err } } nodePath := filepath.Join(ctx.Spool, lastNode.Id.String()) err = tmp.Commit(filepath.Join(nodePath, string(TTx))) - os.Symlink(nodePath, filepath.Join(ctx.Spool, lastNode.Name)) + os.Symlink(nodePath, filepath.Join(ctx.Spool, lastNode.Name)) // #nosec G104 return lastNode, err } @@ -140,7 +140,7 @@ func prepareTxFile(srcPath string) (reader io.Reader, closer io.Closer, fileSize rerr = err return } - os.Remove(src.Name()) + os.Remove(src.Name()) // #nosec G104 tmpW := bufio.NewWriter(src) tmpKey := make([]byte, chacha20poly1305.KeySize) if _, rerr = rand.Read(tmpKey[:]); rerr != nil { @@ -159,13 +159,17 @@ func prepareTxFile(srcPath string) (reader io.Reader, closer io.Closer, fileSize } fileSize = int64(written) if err = tmpW.Flush(); err != nil { + rerr = err + return + } + if _, err = src.Seek(0, io.SeekStart); err != nil { + rerr = err return } - src.Seek(0, io.SeekStart) r, w := io.Pipe() go func() { if _, err := aeadProcess(aead, nonce, false, bufio.NewReader(src), w); err != nil { - w.CloseWithError(err) + w.CloseWithError(err) // #nosec G104 } }() reader = r @@ -244,7 +248,7 @@ func prepareTxFile(srcPath string) (reader io.Reader, closer io.Closer, fileSize closer = DummyCloser{} fileSize += 2 * TarBlockSize // termination block - go func() { + go func() error { tarWr := tar.NewWriter(w) hdr := tar.Header{ Typeflag: tar.TypeDir, @@ -258,7 +262,7 @@ func prepareTxFile(srcPath string) (reader io.Reader, closer io.Closer, fileSize hdr.Name = basePath + e.path[len(rootPath):] hdr.ModTime = e.modTime if err = tarWr.WriteHeader(&hdr); err != nil { - w.CloseWithError(err) + return w.CloseWithError(err) } } hdr.Typeflag = tar.TypeReg @@ -268,20 +272,23 @@ func prepareTxFile(srcPath string) (reader io.Reader, closer io.Closer, fileSize hdr.ModTime = e.modTime hdr.Size = e.size if err = tarWr.WriteHeader(&hdr); err != nil { - w.CloseWithError(err) + return w.CloseWithError(err) } fd, err := os.Open(e.path) if err != nil { - w.CloseWithError(err) + fd.Close() // #nosec G104 + return w.CloseWithError(err) } - _, err = io.Copy(tarWr, bufio.NewReader(fd)) - if err != nil { - w.CloseWithError(err) + if _, err = io.Copy(tarWr, bufio.NewReader(fd)); err != nil { + fd.Close() // #nosec G104 + return w.CloseWithError(err) } - fd.Close() + fd.Close() // #nosec G104 + } + if err = tarWr.Close(); err != nil { + return w.CloseWithError(err) } - tarWr.Close() - w.Close() + return w.Close() }() return } @@ -492,9 +499,11 @@ func (ctx *Ctx) TxExec( if err != nil { return err } - _, err = io.Copy(compressor, in) - compressor.Close() - if err != nil { + if _, err = io.Copy(compressor, in); err != nil { + compressor.Close() // #nosec G104 + return err + } + if err = compressor.Close(); err != nil { return err } size := int64(compressed.Len()) @@ -546,6 +555,6 @@ func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error } else { ctx.LogI("tx", SdsAdd(sds, SDS{"err": err}), "sent") } - os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name)) + os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name)) // #nosec G104 return err }