2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								package hosts
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import (
							 | 
						
					
						
							
								
									
										
										
										
											2018-04-20 11:01:06 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									"context"
							 | 
						
					
						
							
								
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									"net"
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									"github.com/coredns/coredns/plugin"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									"github.com/coredns/coredns/plugin/pkg/dnsutil"
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 16:32:59 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									"github.com/coredns/coredns/plugin/pkg/fall"
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									"github.com/coredns/coredns/request"
							 | 
						
					
						
							
								
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									"github.com/miekg/dns"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// Hosts is the plugin handler
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								type Hosts struct {
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									Next plugin.Handler
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									*Hostsfile
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 14:51:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									Fall fall.F
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// ServeDNS implements the plugin.Handle interface.
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								func (h Hosts) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									state := request.Request{W: w, Req: r}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									qname := state.Name()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									answers := []dns.RR{}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									zone := plugin.Zones(h.Origins).Matches(qname)
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if zone == "" {
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-31 19:37:09 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										// PTR zones don't need to be specified in Origins.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if state.QType() != dns.TypePTR {
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											// if this doesn't match we need to fall through regardless of h.Fallthrough
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											return plugin.NextOrFailure(h.Name(), h.Next, ctx, w, r)
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									switch state.QType() {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									case dns.TypePTR:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										names := h.LookupStaticAddr(dnsutil.ExtractAddressFromReverse(qname))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if len(names) == 0 {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											// If this doesn't match we need to fall through regardless of h.Fallthrough
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											return plugin.NextOrFailure(h.Name(), h.Next, ctx, w, r)
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										answers = h.ptr(qname, h.options.ttl, names)
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									case dns.TypeA:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										ips := h.LookupStaticHostV4(qname)
							 | 
						
					
						
							
								
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										answers = a(qname, h.options.ttl, ips)
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									case dns.TypeAAAA:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										ips := h.LookupStaticHostV6(qname)
							 | 
						
					
						
							
								
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										answers = aaaa(qname, h.options.ttl, ips)
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if len(answers) == 0 {
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 16:32:59 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if h.Fall.Through(qname) {
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											return plugin.NextOrFailure(h.Name(), h.Next, ctx, w, r)
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-31 19:37:09 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										// We want to send an NXDOMAIN, but because of /etc/hosts' setup we don't have a SOA, so we make it REFUSED
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										// to at least give an answer back to signals we're having problems resolving this.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if !h.otherRecordsExist(qname) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											return dns.RcodeServerFailure, nil
							 | 
						
					
						
							
								
									
										
										
										
											2017-07-20 16:09:15 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									m := new(dns.Msg)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									m.SetReply(r)
							 | 
						
					
						
							
								
									
										
										
										
											2018-12-30 17:05:08 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									m.Authoritative = true
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									m.Answer = answers
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									w.WriteMsg(m)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									return dns.RcodeSuccess, nil
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-31 19:37:09 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								func (h Hosts) otherRecordsExist(qname string) bool {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if len(h.LookupStaticHostV4(qname)) > 0 {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										return true
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if len(h.LookupStaticHostV6(qname)) > 0 {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										return true
							 | 
						
					
						
							
								
									
										
										
										
											2017-07-20 16:09:15 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									return false
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// Name implements the plugin.Handle interface.
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								func (h Hosts) Name() string { return "hosts" }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// a takes a slice of net.IPs and returns a slice of A RRs.
							 | 
						
					
						
							
								
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								func a(zone string, ttl uint32, ips []net.IP) []dns.RR {
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									answers := make([]dns.RR, len(ips))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									for i, ip := range ips {
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										r := new(dns.A)
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										r.Hdr = dns.RR_Header{Name: zone, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: ttl}
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										r.A = ip
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										answers[i] = r
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									return answers
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// aaaa takes a slice of net.IPs and returns a slice of AAAA RRs.
							 | 
						
					
						
							
								
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								func aaaa(zone string, ttl uint32, ips []net.IP) []dns.RR {
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									answers := make([]dns.RR, len(ips))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									for i, ip := range ips {
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										r := new(dns.AAAA)
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										r.Hdr = dns.RR_Header{Name: zone, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: ttl}
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										r.AAAA = ip
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										answers[i] = r
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									return answers
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// ptr takes a slice of host names and filters out the ones that aren't in Origins, if specified, and returns a slice of PTR RRs.
							 | 
						
					
						
							
								
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								func (h *Hosts) ptr(zone string, ttl uint32, names []string) []dns.RR {
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									answers := make([]dns.RR, len(names))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									for i, n := range names {
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										r := new(dns.PTR)
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										r.Hdr = dns.RR_Header{Name: zone, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: ttl}
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										r.Ptr = dns.Fqdn(n)
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										answers[i] = r
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									return answers
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 |