summaryrefslogtreecommitdiff
path: root/main.go
blob: 2c99e0e04569334f51a2fec863ec677cf09c20ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/miekg/dns"
	"io/ioutil"
	"net/http"
	"strings"
	"text/template"
	"time"
)

type Zone struct {
	Name string
	View string
}

type TSIGSecrets struct {
	secrets   map[string]map[string]TSIGSecret
	viewzones map[string][]string
}

func NewTSIGSecrets() *TSIGSecrets {
	return &TSIGSecrets{
		secrets:   make(map[string]map[string]TSIGSecret),
		viewzones: make(map[string][]string),
	}
}

func (t *TSIGSecrets) GetViews() []string {
	r := make([]string, 0, len(t.viewzones))
	for k := range t.viewzones {
		r = append(r, k)
	}
	return r
}

func (t *TSIGSecrets) GetViewZones() map[string][]string {
	return t.viewzones
}

func (t *TSIGSecrets) UnmarshalJSON(d []byte) error {
	v := make(map[string]map[string]string)

	if err := json.Unmarshal(d, &v); err != nil {
		return err
	}

	for k, v := range v {
		o := strings.Split(k, "-")
		view := o[len(o)-1]

		d := make([]string, len(o))
		copy(d, o[:len(o)-1])
		dn := strings.Join(d, ".")

		a, ok := v["algorithm"]
		if !ok {
			a = "hmac-sha256."
		}

		if !strings.HasSuffix(a, ".") {
			a = fmt.Sprintf("%s.", a)
		}

		if _, ok := t.viewzones[view]; !ok {
			t.viewzones[view] = make([]string, 1)
			t.viewzones[view] = append(t.viewzones[view], dn)
		} else {
			t.viewzones[view] = append(t.viewzones[view], dn)
		}

		if _, ok := t.secrets[view]; !ok {
			t.secrets[view] = make(map[string]TSIGSecret)
		}

		if !strings.HasSuffix(k, ".") {
			k = fmt.Sprintf("%s.", k)
		}

		t.secrets[view][dn] = TSIGSecret{
			KeyName:   k,
			Algorithm: a,
			Secret:    v["secret"],
		}
	}

	return nil
}

func (t *TSIGSecrets) GetSecret(zone, view string) (*TSIGSecret, error) {
	if !strings.HasSuffix(zone, ".") {
		zone = fmt.Sprintf("%s.", zone)
	}

	if _, ok := t.secrets[view]; !ok {
		return nil, errors.New("No keys for requested zone")
	}

	key, ok := t.secrets[view][zone]
	if !ok {
		return nil, errors.New("No keys for requested view of zone")
	}

	return &key, nil
}

type Signable interface {
	SetTsig(string, string, uint16, int64) *dns.Msg
}

// TODO: Name and Algorithm end with dot (.)
type TSIGSecret struct {
	KeyName   string
	Algorithm string `json:"algorithm"`
	Secret    string `json:"secret"`
}

func (t *TSIGSecret) Sign(r Signable) {
	r.SetTsig(t.KeyName, t.Algorithm, 300, time.Now().Unix())
}

func (t *TSIGSecret) AsMap() map[string]string {
	return map[string]string{
		t.KeyName: t.Secret,
	}
}

func getValue(v interface{}) string {
	switch i := v.(type) {
	case *dns.SOA:
		return fmt.Sprintf("%s %s %d %d %d %d %d", i.Ns, i.Mbox, i.Serial, i.Refresh, i.Retry, i.Expire, i.Minttl)
	case *dns.A:
		return fmt.Sprintf("%s", i.A)
	case *dns.CNAME:
		return fmt.Sprintf("%s", i.Target)
	case *dns.AAAA:
		return fmt.Sprintf("%s", i.AAAA)
	case *dns.MX:
		return fmt.Sprintf("%d %s", i.Preference, i.Mx)
	case *dns.TXT:
		b := &bytes.Buffer{}
		for _, t := range i.Txt {
			fmt.Fprintf(b, "\"%s\"", t) // []
		}
		return b.String()
	case *dns.PTR:
		return fmt.Sprintf("%s", i.Ptr)
	case *dns.NS:
		return fmt.Sprintf("%s", i.Ns)
	case *dns.SRV:
		return fmt.Sprintf("%d %d %d %s", i.Priority, i.Weight, i.Port, i.Target)
	case *dns.SPF:
		b := &bytes.Buffer{}
		for _, t := range i.Txt {
			fmt.Fprintf(b, "\"%s\"", t) // []
		}
		return b.String()
	default:
		return "UNKNOWN"
	}
}

func getDns(sm *TSIGSecrets) chan *dns.Envelope {
	s, _ := sm.GetSecret("crute.us", "external")

	c := &dns.Transfer{}
	c.TsigSecret = s.AsMap()

	m := &dns.Msg{}
	m.SetAxfr("crute.us.")
	s.Sign(m)

	in, err := c.In(m, "172.16.18.52:53")
	if err != nil {
		fmt.Printf("Error: %s\n", err.Error())
		return nil
	}

	return in
}

func dnsClass(rrh *dns.RR_Header) string {
	return dns.Class(rrh.Class).String()
}

func dnsType(rrh *dns.RR_Header) string {
	return dns.Type(rrh.Rrtype).String()
}

func dnsTTL(rrh *dns.RR_Header) string {
	t := rrh.Ttl

	if t/86400 > 1 {
		return fmt.Sprintf("%d days", t/86400)
	} else if t/3600 > 1 {
		return fmt.Sprintf("%d hours", t/3600)
	} else if t/60 > 1 {
		return fmt.Sprintf("%d minutes", t/60)
	} else {
		return fmt.Sprintf("%d seconds", t)
	}
}

func handler(w http.ResponseWriter, r *http.Request) {
	fm := template.FuncMap{
		"getValue": getValue,
		"dnsClass": dnsClass,
		"dnsType":  dnsType,
		"dnsTTL":   dnsTTL,
	}
	t, _ := template.New("").Funcs(fm).ParseFiles("dns.html")
	t.ExecuteTemplate(w, "dns.html", getDns(nil))
}

func main() {
	/*
		http.HandleFunc("/", handler)
		http.ListenAndServe(":8080", nil)
	*/

	c, _ := ioutil.ReadFile("secrets.json")
	k := NewTSIGSecrets()
	json.Unmarshal(c, k)

	//fmt.Printf("%+v\n", k)
	for r := range getDns(k) {
		for _, rr := range r.RR {
			/* hlen := len(rr.Header().String())
			   fmt.Printf("%+v\n", rr.String()[hlen:]) */
		}
	}
}