Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "fmt"
- "log"
- "github.com/globalsign/mgo"
- "github.com/globalsign/mgo/bson"
- "net/url"
- "os"
- "strings"
- )
- /*
- *
- * PowerDNS Pipe Backend
- * For H6G DNS - connecting to MongoDB
- *
- * Recordtypes:
- * A - 1
- * NS - 2
- * CNAME - 5
- * SOA - 6
- * MX - 15
- * TXT - 16
- * AAAA - 28
- * SRV - 33
- * NAPTR - 35
- * CAA - 257
- */
- type dnsrecord struct {
- ID bson.ObjectId `bson:"_id,omitempty"`
- Qtype uint `bson:"type"`
- TTL uint `bson:"TTL"`
- Name string
- Value string
- SOA_p *SOArecord `bson:"SOA"`
- Zone bson.ObjectId `bson:"zone,omitempty"`
- }
- type SOArecord struct {
- Primary string `bson:"primary"`
- Mail string `bson:"mail"`
- Serial bson.ObjectId `bson:"serial,omitempty"`
- Refresh uint `bson:"refresh"`
- Retry uint `bson:"retry"`
- Expire uint `bson:"expire"`
- TTL uint `bson:"TTL"`
- }
- func main() {
- l := log.New(os.Stderr, "", 0)
- if len(os.Args) < 2 {
- l.Printf("Call: %s [DB-Pass]\n", os.Args[0])
- return
- }
- var input string
- stdin := bufio.NewReader(os.Stdin)
- input, _ = stdin.ReadString('\n')
- //Handshake ABI Version 1
- for input != "HELO\t1\n" {
- fmt.Printf("FAIL\n")
- l.Println("Received: ", input)
- input, _ = stdin.ReadString('\n')
- }
- dbsession, err := mgo.Dial(fmt.Sprintf("dns:%s@/var/run/mongodb/mongodb-27017.sock/h6g_dns", url.QueryEscape(os.Args[1])))
- if err != nil {
- fmt.Printf("FAIL\n")
- l.Println("H6GDNS DB: ", err.Error())
- return
- }
- db := dbsession.DB("h6g_dns")
- fmt.Printf("OK\tH6G DNS Connector\n")
- loop:
- for {
- var qtype uint
- var response_p []dnsrecord
- var needle bson.M
- input, _ = stdin.ReadString('\n')
- q := strings.Split(input, "\t")
- if len(q) < 4 {
- fmt.Println("FAIL")
- l.Println("H6GDNS: Input too short")
- continue
- }
- switch q[3] {
- case "ANY":
- qtype = 0
- case "A":
- qtype = 1
- case "NS":
- qtype = 2
- case "CNAME":
- qtype = 5
- case "SOA":
- qtype = 6
- case "MX":
- qtype = 15
- case "TXT":
- qtype = 16
- case "AAAA":
- qtype = 28
- case "SRV":
- qtype = 33
- case "NAPTR":
- qtype = 35
- case "CAA":
- qtype = 257
- default:
- fmt.Println("FAIL")
- l.Println("H6GDNS: unknown qtype")
- continue loop
- }
- if qtype == 0 {
- //ANY Record
- needle = bson.M{"name": q[1]}
- } else {
- needle = bson.M{"name": q[1], "type": qtype}
- }
- query := db.C("dns").Find(needle)
- if n, _ := query.Count(); n > 0 {
- var qtypetxt string
- err = query.All(&response_p)
- if err != nil {
- fmt.Println("FAIL")
- break
- }
- responseloop:
- for _, response := range response_p {
- switch response.Qtype {
- case 1:
- qtypetxt = "A"
- case 2:
- qtypetxt = "NS"
- case 5:
- qtypetxt = "CNAME"
- case 6:
- qtypetxt = "SOA"
- fmt.Printf("DATA\t%s\tIN\t%s\t%d\t-1\t%s %s %d %d %d %d %d\n",
- response.Name, qtypetxt, response.TTL, response.SOA_p.Primary,
- response.SOA_p.Mail, response.SOA_p.Serial.Time().Unix(), response.SOA_p.Refresh,
- response.SOA_p.Retry, response.SOA_p.Expire, response.SOA_p.TTL)
- continue responseloop
- case 15:
- qtypetxt = "MX"
- case 16:
- qtypetxt = "TXT"
- case 28:
- qtypetxt = "AAAA"
- case 33:
- qtypetxt = "SRV"
- case 35:
- qtypetxt = "NAPTR"
- case 257:
- qtypetxt = "CAA"
- }
- fmt.Printf("DATA\t%s\tIN\t%s\t%d\t-1\t%s\n", response.Name, qtypetxt, response.TTL, response.Value)
- }
- }
- fmt.Println("END")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement