Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement target LOG #21

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,26 @@ func TestParser_Parse(t *testing.T) {
},
err: nil,
},
{
name: "parse log",
s: "-A foo -j LOG --log-prefix \"foo\" --log-level emerg --log-tcp-sequence --log-tcp-options --log-ip-options --log-uid --log-macdecode",
r: Rule{
Chain: "foo",
Jump: &Target{
Name: "LOG",
Flags: map[string]Flag{
"log-prefix": {Values: []string{"foo"}},
"log-level": {Values: []string{"emerg"}},
"log-tcp-sequence": {},
"log-tcp-options": {},
"log-ip-options": {},
"log-uid": {},
"log-macdecode": {},
},
},
},
err: nil,
},
} {
t.Run(tc.name, func(t *testing.T) {
p := NewParser(strings.NewReader(tc.s))
Expand Down
63 changes: 63 additions & 0 deletions targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func (p *Parser) parseTarget(t *Target) (state, error) {
s, err = p.parseSNAT(&t.Flags)
case "MASQUERADE":
s, err = p.parseMASQUERADE(&t.Flags)
case "LOG":
s, err = p.parseLOG(&t.Flags)
default:
s = sError
err = fmt.Errorf("target %q is not implemented", lit)
Expand Down Expand Up @@ -194,3 +196,64 @@ func (p *Parser) parseDNAT(f *map[string]Flag) (state, error) {
p.unscan(1) // unscan the last rune, so main parser can interprete it
return sStart, nil
}

func (p *Parser) parseLOG(f *map[string]Flag) (state, error) {
s := sStart
for tok, lit := p.scanIgnoreWhitespace(); tok != EOF && tok != NEWLINE; tok, lit = p.scanIgnoreWhitespace() {
nextValue := false
for !nextValue {
nextValue = true
switch s {
case sStart:
switch tok {
case FLAG:
s = sIF
nextValue = false
default:
// No more flags
p.unscan(1)
return sStart, nil
}
case sIF:
switch {
case lit == "--log-prefix":
_, lit := p.scanIgnoreWhitespace()
(*f)["log-prefix"] = Flag{
Values: []string{lit},
}
s = sStart
case lit == "--log-level":
_, lit := p.scanIgnoreWhitespace()
(*f)["log-level"] = Flag{
Values: []string{lit},
}
s = sStart
case lit == "--log-tcp-sequence":
(*f)["log-tcp-sequence"] = Flag{}
s = sStart
case lit == "--log-tcp-options":
(*f)["log-tcp-options"] = Flag{}
s = sStart
case lit == "--log-ip-options":
(*f)["log-ip-options"] = Flag{}
s = sStart
case lit == "--log-macdecode":
(*f)["log-macdecode"] = Flag{}
s = sStart
case lit == "--log-uid":
(*f)["log-uid"] = Flag{}
s = sStart
default:
// The end of the match statement is reached.
p.unscan(1)
return sStart, nil
}

default:
return sStart, errors.New("unexpected error parsing match extension")
}
}
}
p.unscan(1) // unscan the last rune, so main parser can interprete it
return sStart, nil
}
Loading