Skip to content

Commit

Permalink
Better user input validation, always clear filter slice
Browse files Browse the repository at this point in the history
  • Loading branch information
Kioubit committed Jan 9, 2022
1 parent a7eb52c commit 49c6c33
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bufio"
"fmt"
"log"
"os"
"pndpd/modules"
"pndpd/pndp"
Expand All @@ -13,7 +12,8 @@ import (
func readConfig(dest string) {
file, err := os.Open(dest)
if err != nil {
log.Fatal(err)
fmt.Println("Error:", err.Error())
os.Exit(1)
}
defer func(file *os.File) {
_ = file.Close()
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var Version = "Development"

func main() {
fmt.Println("PNDPD Version", Version, "- Kioubit 2021")
fmt.Println("PNDPD Version", Version, "- Kioubit 2022")

if len(os.Args) <= 2 {
printUsage()
Expand Down Expand Up @@ -53,7 +53,7 @@ func printUsage() {
for i := range modules.ModuleList {
for d := range (*modules.ModuleList[i]).Commands {
if (*modules.ModuleList[i]).Commands[d].CommandLineEnabled {
fmt.Println("pndpd", (*modules.ModuleList[i]).Commands[d].Description)
fmt.Println((*modules.ModuleList[i]).Commands[d].Description)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions modules/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
// This is an example module
func init() {
commands := []modules.Command{{
CommandText: "command1",
CommandText: "pndpd command1",
Description: "This is the usage description for command1",
BlockTerminate: true,
CommandLineEnabled: true,
ConfigEnabled: true,
}, {
CommandText: "command2",
CommandText: "pndpd command2",
Description: "This is the usage description for command2",
BlockTerminate: false,
CommandLineEnabled: false,
Expand Down
36 changes: 30 additions & 6 deletions modules/userInterface/userInterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package userInterface

import (
"fmt"
"os"
"pndpd/modules"
"pndpd/pndp"
"strings"
Expand All @@ -13,19 +14,19 @@ import (
func init() {
commands := []modules.Command{{
CommandText: "proxy",
Description: "proxy <interface1> <interface2> <optional whitelist of CIDRs separated by a semicolon applied to interface2>",
Description: "pndpd proxy <interface1> <interface2> <optional whitelist of CIDRs separated by a semicolon applied to interface2>",
BlockTerminate: true,
ConfigEnabled: true,
CommandLineEnabled: true,
}, {
CommandText: "responder",
Description: "responder <interface> <optional whitelist of CIDRs separated by a semicolon>",
Description: "pndpd responder <interface> <optional whitelist of CIDRs separated by a semicolon>",
BlockTerminate: true,
ConfigEnabled: true,
CommandLineEnabled: true,
}, {
CommandText: "modules",
Description: "modules available - list available modules",
Description: "pndpd modules available - list available modules",
BlockTerminate: false,
ConfigEnabled: false,
CommandLineEnabled: true,
Expand Down Expand Up @@ -114,14 +115,23 @@ func initCallback(callback modules.CallbackInfo) {
if strings.HasPrefix(n, "filter") {
filter += strings.TrimSpace(strings.TrimPrefix(n, "filter")) + ";"
if strings.Contains(n, ";") {
panic("Invalid config file syntax")
showError("config: the use of semicolons is not allowed in the filter arguments")
}
}
if strings.HasPrefix(n, "autosense") {
obj.autosense = strings.TrimSpace(strings.TrimPrefix(n, "autosense"))
}
if strings.Contains(n, "//") {
showError("config: comments are not allowed after arguments")
}
}
obj.Filter = strings.TrimSuffix(filter, ";")
if obj.autosense != "" && obj.Filter != "" {
showError("config: cannot have both a filter and autosense enabled on a proxy object")
}
if obj.Iface2 == "" || obj.Iface1 == "" {
showError("config: two interfaces need to be specified in the config file for a proxy object. (iface1 and iface2 parameters)")
}
allProxies = append(allProxies, &obj)
case "responder":
obj := configResponder{}
Expand All @@ -133,13 +143,21 @@ func initCallback(callback modules.CallbackInfo) {
if strings.HasPrefix(n, "filter") {
filter += strings.TrimSpace(strings.TrimPrefix(n, "filter")) + ";"
if strings.Contains(n, ";") {
panic("Invalid config file syntax")
showError("config: the use of semicolons is not allowed in the filter arguments")
}
}
if strings.HasPrefix(n, "autosense") {
obj.autosense = strings.TrimSpace(strings.TrimPrefix(n, "autosense"))
}

if obj.autosense != "" && obj.Filter != "" {
showError("config: cannot have both a filter and autosense enabled on a responder object")
}
if obj.Iface == "" {
showError("config: interface not specified in the responder object. (iface parameter)")
}
if strings.Contains(n, "//") {
showError("config: comments are not allowed after arguments")
}
}
obj.Filter = strings.TrimSuffix(filter, ";")
allResponders = append(allResponders, &obj)
Expand Down Expand Up @@ -169,3 +187,9 @@ func shutdownCallback() {
n.instance.Stop()
}
}

func showError(error string) {
fmt.Println(error)
fmt.Println("Exiting due to error")
os.Exit(1)
}
8 changes: 5 additions & 3 deletions pndp/process.go → pndp/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type ProxyObj struct {
// With the optional autosenseInterface argument, the whitelist is configured based on the addresses assigned to the interface specified. This works even if the IP addresses change frequently.
// Start() must be called on the object to actually start responding
func NewResponder(iface string, filter []*net.IPNet, autosenseInterface string) *ResponderObj {
if filter == nil {
if filter == nil && autosenseInterface == "" {
fmt.Println("WARNING: You should use a whitelist for the responder unless you really know what you are doing")
}
var s sync.WaitGroup
Expand All @@ -59,7 +59,8 @@ func (obj *ResponderObj) start() {
}()
go respond(obj.iface, requests, ndp_ADV, nil, obj.filter, obj.autosense, obj.stopWG, obj.stopChan)
go listen(obj.iface, requests, ndp_SOL, obj.stopWG, obj.stopChan)
fmt.Println("Started responder instance on interface", obj.iface)
fmt.Printf("Started responder instance on interface %s", obj.iface)
fmt.Println()
<-obj.stopChan
}

Expand Down Expand Up @@ -130,7 +131,8 @@ func (obj *ProxyObj) start() {
go listen(obj.iface2, req_iface2_adv_iface1, ndp_ADV, obj.stopWG, obj.stopChan)
go respond(obj.iface1, req_iface2_adv_iface1, ndp_ADV, out_iface2_sol_questions_iface1_adv, nil, "", obj.stopWG, obj.stopChan)

fmt.Println("Started Proxy instance for interfaces:", obj.iface1, "and", obj.iface2)
fmt.Printf("Started Proxy instance on interfaces %s and %s (if enabled, the whitelist is applied on %s)", obj.iface1, obj.iface2, obj.iface2)
fmt.Println()
<-obj.stopChan
}

Expand Down
1 change: 1 addition & 0 deletions pndp/responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func respond(iface string, requests chan *ndpRequest, respondType ndpType, ndpQu
// Auto-sense
if autoSense != "" {
//TODO Future work: Use another sub goroutine to monitor the interface instead of checking here
filter = make([]*net.IPNet, 0)
result = selectSourceIP(respondIface)
autoiface, err := net.InterfaceByName(autoSense)
if err != nil {
Expand Down

0 comments on commit 49c6c33

Please sign in to comment.