Advertisement
pzillmann

Untitled

Jan 10th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.16 KB | None | 0 0
  1.     package main
  2.  
  3.     import (
  4.         "bufio"
  5.         "fmt"
  6.         "log"
  7.         "github.com/globalsign/mgo"
  8.         "github.com/globalsign/mgo/bson"
  9.         "net/url"
  10.         "os"
  11.         "strings"
  12.     )
  13.  
  14.     /*
  15.      *
  16.      * PowerDNS Pipe Backend
  17.      * For H6G DNS - connecting to MongoDB
  18.      *
  19.  
  20.      * Recordtypes:
  21.      * A - 1
  22.      * NS - 2
  23.      * CNAME - 5
  24.      * SOA - 6
  25.      * MX - 15
  26.      * TXT - 16
  27.      * AAAA - 28
  28.      * SRV - 33
  29.      * NAPTR - 35
  30.      * CAA - 257
  31.      */
  32.  
  33.     type dnsrecord struct {
  34.         ID    bson.ObjectId `bson:"_id,omitempty"`
  35.         Qtype uint          `bson:"type"`
  36.         TTL   uint          `bson:"TTL"`
  37.         Name  string
  38.         Value string
  39.         SOA_p *SOArecord    `bson:"SOA"`
  40.         Zone  bson.ObjectId `bson:"zone,omitempty"`
  41.     }
  42.  
  43.     type SOArecord struct {
  44.         Primary string        `bson:"primary"`
  45.         Mail    string        `bson:"mail"`
  46.         Serial  bson.ObjectId `bson:"serial,omitempty"`
  47.         Refresh uint          `bson:"refresh"`
  48.         Retry   uint          `bson:"retry"`
  49.         Expire  uint          `bson:"expire"`
  50.         TTL     uint          `bson:"TTL"`
  51.     }
  52.  
  53.     func main() {
  54.         l := log.New(os.Stderr, "", 0)
  55.  
  56.         if len(os.Args) < 2 {
  57.             l.Printf("Call: %s [DB-Pass]\n", os.Args[0])
  58.             return
  59.         }
  60.  
  61.         var input string
  62.         stdin := bufio.NewReader(os.Stdin)
  63.         input, _ = stdin.ReadString('\n')
  64.  
  65.         //Handshake ABI Version 1
  66.         for input != "HELO\t1\n" {
  67.             fmt.Printf("FAIL\n")
  68.             l.Println("Received: ", input)
  69.             input, _ = stdin.ReadString('\n')
  70.         }
  71.  
  72.         dbsession, err := mgo.Dial(fmt.Sprintf("dns:%s@/var/run/mongodb/mongodb-27017.sock/h6g_dns", url.QueryEscape(os.Args[1])))
  73.         if err != nil {
  74.             fmt.Printf("FAIL\n")
  75.             l.Println("H6GDNS DB: ", err.Error())
  76.             return
  77.         }
  78.         db := dbsession.DB("h6g_dns")
  79.  
  80.         fmt.Printf("OK\tH6G DNS Connector\n")
  81.  
  82.     loop:
  83.         for {
  84.             var qtype uint
  85.             var response_p []dnsrecord
  86.             var needle bson.M
  87.             input, _ = stdin.ReadString('\n')
  88.             q := strings.Split(input, "\t")
  89.  
  90.             if len(q) < 4 {
  91.                 fmt.Println("FAIL")
  92.                 l.Println("H6GDNS: Input too short")
  93.                 continue
  94.             }
  95.  
  96.             switch q[3] {
  97.             case "ANY":
  98.                 qtype = 0
  99.             case "A":
  100.                 qtype = 1
  101.             case "NS":
  102.                 qtype = 2
  103.             case "CNAME":
  104.                 qtype = 5
  105.             case "SOA":
  106.                 qtype = 6
  107.             case "MX":
  108.                 qtype = 15
  109.             case "TXT":
  110.                 qtype = 16
  111.             case "AAAA":
  112.                 qtype = 28
  113.             case "SRV":
  114.                 qtype = 33
  115.             case "NAPTR":
  116.                 qtype = 35
  117.             case "CAA":
  118.                 qtype = 257
  119.             default:
  120.                 fmt.Println("FAIL")
  121.                 l.Println("H6GDNS: unknown qtype")
  122.                 continue loop
  123.             }
  124.  
  125.             if qtype == 0 {
  126.                 //ANY Record
  127.                 needle = bson.M{"name": q[1]}
  128.             } else {
  129.                 needle = bson.M{"name": q[1], "type": qtype}
  130.             }
  131.  
  132.             query := db.C("dns").Find(needle)
  133.  
  134.             if n, _ := query.Count(); n > 0 {
  135.                 var qtypetxt string
  136.                 err = query.All(&response_p)
  137.                 if err != nil {
  138.                     fmt.Println("FAIL")
  139.                     break
  140.                 }
  141.  
  142.             responseloop:
  143.                 for _, response := range response_p {
  144.  
  145.                     switch response.Qtype {
  146.                     case 1:
  147.                         qtypetxt = "A"
  148.                     case 2:
  149.                         qtypetxt = "NS"
  150.                     case 5:
  151.                         qtypetxt = "CNAME"
  152.                     case 6:
  153.                         qtypetxt = "SOA"
  154.                         fmt.Printf("DATA\t%s\tIN\t%s\t%d\t-1\t%s %s %d %d %d %d %d\n",
  155.                             response.Name, qtypetxt, response.TTL, response.SOA_p.Primary,
  156.                             response.SOA_p.Mail, response.SOA_p.Serial.Time().Unix(), response.SOA_p.Refresh,
  157.                             response.SOA_p.Retry, response.SOA_p.Expire, response.SOA_p.TTL)
  158.                         continue responseloop
  159.                     case 15:
  160.                         qtypetxt = "MX"
  161.                     case 16:
  162.                         qtypetxt = "TXT"
  163.                     case 28:
  164.                         qtypetxt = "AAAA"
  165.                     case 33:
  166.                         qtypetxt = "SRV"
  167.                     case 35:
  168.                         qtypetxt = "NAPTR"
  169.                     case 257:
  170.                         qtypetxt = "CAA"
  171.                     }
  172.  
  173.                     fmt.Printf("DATA\t%s\tIN\t%s\t%d\t-1\t%s\n", response.Name, qtypetxt, response.TTL, response.Value)
  174.                 }
  175.             }
  176.  
  177.             fmt.Println("END")
  178.  
  179.         }
  180.  
  181.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement