commit 7dfe464f2e85d9e9fecf5864a09f5fedbab52158 Author: Darko Luketic Date: Fri Aug 30 18:44:36 2019 +0200 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..95220cc --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2019 Darko Luketic + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..8ffab87 --- /dev/null +++ b/README.md @@ -0,0 +1,64 @@ +# spamip + +spamip is a command line tool that adds an IP to a bind zone file in DNSBL format + +## Getting Started + +All you need is a file with this header, in my cast it's located in `/var/bind/pri/localhost.zone` + +``` +$TTL 1W +@ IN SOA localhost. root.localhost. ( + 2009121301 ; Serial + 28800 ; Refresh + 14400 ; Retry + 604800 ; Expire - 1 week + 86400 ) ; Minimum +@ IN NS localhost. +@ IN A 127.0.0.1 + +@ IN AAAA ::1 +;------------------------------------------------------------------ +2.0.0.127 IN A 127.0.0.2 + IN TXT "example.com test record" +3.0.0.127 IN A 127.0.0.3 + IN TXT "example.com verified spam source" +10.0.0.127 IN A 127.0.0.10 + IN TXT "example.com confirmed DUL range" +;------------------------------------------------------------------ +``` + +and this part appended to your /etc/bind/named.conf + +``` +zone "localhost" IN { + type master; + file "pri/localhost.zone"; + notify no; +}; +``` + +Then check your message source (in Thunderbird that's CTRL+U) for the spammer's IP you'd like to block +and write `spamip add 1.2.3.4` where 1.2.3.4 would be the spammer's IPv4 address. + +and finally reload bind (`systemctl reload named.service` or bind9.service or bind.service) + +The postfix part is simple: +``` +smtpd_recipient_restrictions = + reject_unknown_sender_domain, + reject_unknown_recipient_domain, + reject_non_fqdn_sender, + reject_unauth_pipelining, + permit_mynetworks, + permit_sasl_authenticated, + reject_unauth_destination, + reject_rbl_client localhost +``` + +et vĂ³ila, your own DNSBL. + +## Licence + +MIT + diff --git a/main.go b/main.go new file mode 100644 index 0000000..6610f6c --- /dev/null +++ b/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "errors" + "fmt" + "log" + "os" + "regexp" + "time" + + "github.com/urfave/cli" +) + +const ( + zoneFilePath = "/var/bind/pri/localhost.zone" +) + +var re = regexp.MustCompile(`^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$`) + +func main() { + app := cli.NewApp() + app.Name = "spamip" + app.Version = "0.1" + app.Compiled = time.Now() + app.Authors = []cli.Author{ + { + Name: "Darko Luketic", + Email: "info@icod.de", + }, + } + app.Usage = "spamip is a tool that adds an IP to a bind zone file in DNSBL format" + + app.Flags = []cli.Flag{ + cli.StringFlag{ + Name: "file, f", + Usage: "path/filename of the zone file", + EnvVar: "SPAMIP_FILE", + Required: false, + Hidden: false, + TakesFile: false, + Value: zoneFilePath, + }, + } + + app.Commands = []cli.Command{ + { + Name: "add", + Aliases: []string{"a"}, + Usage: "add an IP address to the zone file", + Action: func(c *cli.Context) error { + if len(c.Args()) != 1 { + return errors.New("add takes exactly 1 argument, an IPv4 address") + } + if !re.MatchString(c.Args().First()) { + return errors.New(c.Args().First() + "is not an IPv4 address") + } + s := re.FindStringSubmatch(c.Args().First()) + text := fmt.Sprintf("%s.%s.%s.%s\tIN\tA\t127.0.0.3\n", s[4], s[3], s[2], s[1]) + return appendText(text, c.GlobalString("file")) + }, + }, + } + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } +} + +func appendText(text, file string) error { + f, e := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if e != nil { + return e + } + if n, e := f.WriteString(text); e != nil { + return e + } else if n != len(text) { + return errors.New("did not write all the text") + } + return f.Close() +}