summaryrefslogtreecommitdiff
path: root/api/dns/dns.go
blob: 4cda557b804e697e70acf42137dc7d78a06a2233 (plain) (blame)
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
package dns

import (
	"log"
	"net/http"
	"strconv"
	"strings"

	"penguins.lan/api/types"
	"penguins.lan/database"
	"penguins.lan/utils"
)

const MaxUserRecords = 50

var allowedTypes = map[string]bool{"A": true, "AAAA": true, "CNAME": true, "TXT": true}

// ListDNSRecordsContinuation loads the current user's records for the editor.
func ListDNSRecordsContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
	return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
		if context.User == nil {
			return failure(context, req, resp)
		}
		records, err := database.GetUserDNSRecords(context.DBConn, context.User.ID)
		if err != nil {
			log.Println("dns: list records failed:", err)
			resp.WriteHeader(http.StatusInternalServerError)
			return failure(context, req, resp)
		}
		(*context.TemplateData)["DNSRecords"] = records
		return success(context, req, resp)
	}
}

// CreateDNSRecordContinuation validates and upserts a record. Names are confined
// to the user's own <username>.<zone> subtree; bare/relative names are expanded
// to it for convenience.
func CreateDNSRecordContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
	return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
		fail := func(msg string, code int) types.ContinuationChain {
			resp.WriteHeader(code)
			(*context.TemplateData)["Error"] = types.BannerMessages{Messages: []string{msg}}
			return failure(context, req, resp)
		}

		recordType := strings.ToUpper(strings.TrimSpace(req.FormValue("type")))
		if !allowedTypes[recordType] {
			return fail("type must be one of A, AAAA, CNAME, TXT", http.StatusBadRequest)
		}

		name, ok := qualifyName(req.FormValue("name"), context.User.Username, context.Args.DnsZone)
		if !ok {
			return fail("name must be under "+context.User.Username+"."+context.Args.DnsZone, http.StatusBadRequest)
		}

		content := strings.TrimSpace(req.FormValue("content"))
		if content == "" {
			return fail("content can't be empty", http.StatusBadRequest)
		}

		ttl := 300
		if raw := strings.TrimSpace(req.FormValue("ttl")); raw != "" {
			n, err := strconv.Atoi(raw)
			if err != nil || n < 0 {
				return fail("ttl must be a non-negative number", http.StatusBadRequest)
			}
			ttl = n
		}

		count, err := database.CountUserDNSRecords(context.DBConn, context.User.ID)
		if err != nil {
			return fail("something broke, try again", http.StatusInternalServerError)
		}
		if count >= MaxUserRecords {
			return fail("you've hit the record limit", http.StatusTooManyRequests)
		}

		// A name may hold at most one CNAME; replace any existing one so
		// repointing the primary (or re-running the wizard) doesn't duplicate.
		if recordType == "CNAME" {
			if err := database.DeleteUserRecordsByNameType(context.DBConn, context.User.ID, name, "CNAME"); err != nil {
				return fail("something broke, try again", http.StatusInternalServerError)
			}
		}

		record := &database.DNSRecord{
			ID:      utils.RandomId(),
			UserID:  context.User.ID,
			Name:    name,
			Type:    recordType,
			Content: content,
			TTL:     ttl,
		}
		if _, err := database.SaveDNSRecord(context.DBConn, record); err != nil {
			log.Println("dns: save record failed:", err)
			return fail("couldn't save that record", http.StatusInternalServerError)
		}

		http.Redirect(resp, req, "/profile", http.StatusSeeOther)
		return success(context, req, resp)
	}
}

// DeleteDNSRecordContinuation removes a record the caller owns.
func DeleteDNSRecordContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
	return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
		record, err := database.GetDNSRecord(context.DBConn, req.FormValue("id"))
		if err != nil {
			resp.WriteHeader(http.StatusNotFound)
			return failure(context, req, resp)
		}
		if record.UserID != context.User.ID {
			resp.WriteHeader(http.StatusUnauthorized)
			return failure(context, req, resp)
		}
		if err := database.DeleteDNSRecord(context.DBConn, record.ID); err != nil {
			log.Println("dns: delete record failed:", err)
			resp.WriteHeader(http.StatusInternalServerError)
			return failure(context, req, resp)
		}
		http.Redirect(resp, req, "/profile", http.StatusSeeOther)
		return success(context, req, resp)
	}
}

// qualifyName normalizes a record name to a fully-qualified name (trailing dot)
// confined to the user's <username>.<zone> subtree. A name already under the
// subtree is kept; a name elsewhere in the zone is rejected; anything else is
// treated as relative to the subtree (so "www" -> "www.<user>.<zone>.").
func qualifyName(raw, username, zone string) (string, bool) {
	raw = strings.ToLower(strings.TrimSpace(raw))
	raw = strings.TrimSuffix(raw, ".")
	if raw == "" {
		return "", false
	}
	zone = strings.ToLower(zone)
	userDomain := username + "." + zone

	switch {
	case raw == userDomain || strings.HasSuffix(raw, "."+userDomain):
		return raw + ".", true
	case raw == zone || strings.HasSuffix(raw, "."+zone):
		return "", false // in the zone but not the user's subtree
	default:
		return raw + "." + userDomain + ".", true
	}
}