]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-xfer/main.go
Fix encrypted packet magic number check
[nncp.git] / src / cmd / nncp-xfer / main.go
index ae47ec6a842687e9cfb20d733d77a27427acdb56..41e8c61c8e157f087d9fa5096990d92231bf316d 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2022 Sergey Matveev <stargrave@stargrave.org>
 
 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
@@ -29,7 +29,7 @@ import (
        "path/filepath"
 
        "github.com/dustin/go-humanize"
-       "go.cypherpunks.ru/nncp/v6"
+       "go.cypherpunks.ru/nncp/v8"
 )
 
 func usage() {
@@ -141,7 +141,7 @@ func main() {
                goto Tx
        }
        fis, err = dir.Readdir(0)
-       dir.Close() // #nosec G104
+       dir.Close()
        if err != nil {
                ctx.LogE("xfer-self-read", les, err, func(les nncp.LEs) string {
                        return logMsg(les) + ": reading"
@@ -185,7 +185,7 @@ func main() {
                        continue
                }
                fisInt, err := dir.Readdir(0)
-               dir.Close() // #nosec G104
+               dir.Close()
                if err != nil {
                        ctx.LogE("xfer-rx-read", les, err, func(les nncp.LEs) string {
                                return logMsg(les) + ": reading"
@@ -218,18 +218,39 @@ func main() {
                                continue
                        }
                        pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
-                       if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
-                               ctx.LogD("xfer-rx-not-packet", les, func(les nncp.LEs) string {
-                                       return logMsg(les) + ": is not a packet"
-                               })
-                               fd.Close() // #nosec G104
+                       if err == nil {
+                               switch pktEnc.Magic {
+                               case nncp.MagicNNCPEv1.B:
+                                       err = nncp.MagicNNCPEv1.TooOld()
+                               case nncp.MagicNNCPEv2.B:
+                                       err = nncp.MagicNNCPEv2.TooOld()
+                               case nncp.MagicNNCPEv3.B:
+                                       err = nncp.MagicNNCPEv3.TooOld()
+                               case nncp.MagicNNCPEv4.B:
+                                       err = nncp.MagicNNCPEv4.TooOld()
+                               case nncp.MagicNNCPEv5.B:
+                                       err = nncp.MagicNNCPEv5.TooOld()
+                               case nncp.MagicNNCPEv6.B:
+                               default:
+                                       err = errors.New("is not an encrypted packet")
+                               }
+                       }
+                       if err != nil {
+                               ctx.LogD(
+                                       "xfer-rx-not-packet",
+                                       append(les, nncp.LE{K: "Err", V: err}),
+                                       func(les nncp.LEs) string {
+                                               return logMsg(les) + ": not valid packet: " + err.Error()
+                                       },
+                               )
+                               fd.Close()
                                continue
                        }
                        if pktEnc.Nice > nice {
                                ctx.LogD("xfer-rx-too-nice", les, func(les nncp.LEs) string {
                                        return logMsg(les) + ": too nice"
                                })
-                               fd.Close() // #nosec G104
+                               fd.Close()
                                continue
                        }
                        les = append(les, nncp.LE{K: "Size", V: fiInt.Size()})
@@ -242,7 +263,7 @@ func main() {
                        }
                        if !ctx.IsEnoughSpace(fiInt.Size()) {
                                ctx.LogE("xfer-rx", les, errors.New("is not enough space"), logMsg)
-                               fd.Close() // #nosec G104
+                               fd.Close()
                                continue
                        }
                        if _, err = fd.Seek(0, 0); err != nil {
@@ -260,7 +281,7 @@ func main() {
                                }
                                if err != nil {
                                        ctx.LogE("xfer-rx", les, err, logMsg)
-                                       w.CloseWithError(err) // #nosec G104
+                                       w.CloseWithError(err)
                                }
                        }()
                        if _, err = nncp.CopyProgressed(
@@ -275,7 +296,7 @@ func main() {
                                ctx.LogE("xfer-rx", les, err, logMsg)
                                isBad = true
                        }
-                       fd.Close() // #nosec G104
+                       fd.Close()
                        if isBad {
                                tmp.Cancel()
                                continue
@@ -404,7 +425,9 @@ Tx:
                                })
                                continue
                        }
-                       if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
+                       if _, err = os.Stat(filepath.Join(
+                               dstPath, nncp.SeenDir, pktName,
+                       )); err == nil || !os.IsNotExist(err) {
                                ctx.LogD("xfer-tx-seen", les, func(les nncp.LEs) string {
                                        return logMsg(les) + ": already seen"
                                })
@@ -427,7 +450,7 @@ Tx:
                                ctx.LogE("xfer-tx-open", les, err, func(les nncp.LEs) string {
                                        return logMsg(les) + ": opening"
                                })
-                               tmp.Close() // #nosec G104
+                               tmp.Close()
                                isBad = true
                                continue
                        }
@@ -437,17 +460,17 @@ Tx:
                                append(les, nncp.LE{K: "FullSize", V: job.Size}),
                                ctx.ShowPrgrs,
                        )
-                       fd.Close() // #nosec G104
+                       fd.Close()
                        if err != nil {
                                ctx.LogE("xfer-tx-copy", les, err, func(les nncp.LEs) string {
                                        return logMsg(les) + ": copying"
                                })
-                               tmp.Close() // #nosec G104
+                               tmp.Close()
                                isBad = true
                                continue
                        }
                        if err = bufW.Flush(); err != nil {
-                               tmp.Close() // #nosec G104
+                               tmp.Close()
                                ctx.LogE("xfer-tx-flush", les, err, func(les nncp.LEs) string {
                                        return logMsg(les) + ": flushing"
                                })
@@ -455,7 +478,7 @@ Tx:
                                continue
                        }
                        if err = tmp.Sync(); err != nil {
-                               tmp.Close() // #nosec G104
+                               tmp.Close()
                                ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string {
                                        return logMsg(les) + ": syncing"
                                })
@@ -481,7 +504,7 @@ Tx:
                                isBad = true
                                continue
                        }
-                       os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
+                       os.Remove(filepath.Join(dstPath, pktName+".part"))
                        les = les[:len(les)-1]
                        ctx.LogI(
                                "xfer-tx",
@@ -499,7 +522,7 @@ Tx:
                                        })
                                        isBad = true
                                } else if ctx.HdrUsage {
-                                       os.Remove(job.Path + nncp.HdrSuffix)
+                                       os.Remove(nncp.JobPath2Hdr(job.Path))
                                }
                        }
                }