| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package file
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2016-04-14 19:57:39 +01:00
										 |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"os"
 | 
					
						
							| 
									
										
										
										
											2016-10-11 20:42:28 +01:00
										 |  |  | 	"path"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	"github.com/miekg/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-11-09 21:26:49 +00:00
										 |  |  | 	"github.com/miekg/coredns/middleware/proxy"
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func init() {
 | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("file", caddy.Plugin{
 | 
					
						
							|  |  |  | 		ServerType: "dns",
 | 
					
						
							|  |  |  | 		Action:     setup,
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	zones, err := fileParse(c)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | 		return middleware.Error("file", err)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 	// Add startup functions to notify the master(s).
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 	for _, n := range zones.Names {
 | 
					
						
							| 
									
										
										
										
											2016-10-18 00:04:18 +08:00
										 |  |  | 		z := zones.Z[n]
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		c.OnStartup(func() error {
 | 
					
						
							| 
									
										
										
										
											2016-10-18 00:04:18 +08:00
										 |  |  | 			z.StartupOnce.Do(func() {
 | 
					
						
							|  |  |  | 				if len(z.TransferTo) > 0 {
 | 
					
						
							|  |  |  | 					z.Notify()
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 				z.Reload()
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 			})
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 			return nil
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 11:26:00 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		return File{Next: next, Zones: zones}
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	return nil
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func fileParse(c *caddy.Controller) (Zones, error) {
 | 
					
						
							|  |  |  | 	z := make(map[string]*Zone)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	names := []string{}
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	origins := []string{}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 20:42:28 +01:00
										 |  |  | 	config := dnsserver.GetConfig(c)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		if c.Val() == "file" {
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 			// file db.file [zones...]
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			if !c.NextArg() {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 				return Zones{}, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 			fileName := c.Val()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 			origins = make([]string, len(c.ServerBlockKeys))
 | 
					
						
							|  |  |  | 			copy(origins, c.ServerBlockKeys)
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 			args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 			if len(args) > 0 {
 | 
					
						
							|  |  |  | 				origins = args
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 20:42:28 +01:00
										 |  |  | 			if !path.IsAbs(fileName) && config.Root != "" {
 | 
					
						
							|  |  |  | 				fileName = path.Join(config.Root, fileName)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 			reader, err := os.Open(fileName)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-27 10:48:22 +00:00
										 |  |  | 				// bail out
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 				return Zones{}, err
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 			for i := range origins {
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 				origins[i] = middleware.Host(origins[i]).Normalize()
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 				zone, err := Parse(reader, origins[i], fileName)
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 				if err == nil {
 | 
					
						
							|  |  |  | 					z[origins[i]] = zone
 | 
					
						
							| 
									
										
										
										
											2016-04-27 10:48:22 +00:00
										 |  |  | 				} else {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 					return Zones{}, err
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 				}
 | 
					
						
							|  |  |  | 				names = append(names, origins[i])
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 			noReload := false
 | 
					
						
							| 
									
										
										
										
											2016-11-09 21:26:49 +00:00
										 |  |  | 			prxy := proxy.Proxy{}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 			for c.NextBlock() {
 | 
					
						
							| 
									
										
										
										
											2016-10-02 19:41:00 +01:00
										 |  |  | 				t, _, e := TransferParse(c, false)
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 				if e != nil {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 					return Zones{}, e
 | 
					
						
							| 
									
										
										
										
											2016-03-28 12:08:05 +01:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 				switch c.Val() {
 | 
					
						
							|  |  |  | 				case "no_reload":
 | 
					
						
							|  |  |  | 					noReload = true
 | 
					
						
							| 
									
										
										
										
											2016-11-09 21:26:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				case "upstream":
 | 
					
						
							|  |  |  | 					args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 					if len(args) == 0 {
 | 
					
						
							|  |  |  | 						return Zones{}, c.ArgErr()
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							|  |  |  | 					for i := 0; i < len(args); i++ {
 | 
					
						
							|  |  |  | 						h, p, e := net.SplitHostPort(args[i])
 | 
					
						
							|  |  |  | 						if e != nil && p == "" {
 | 
					
						
							|  |  |  | 							args[i] = h + ":53"
 | 
					
						
							|  |  |  | 						}
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							|  |  |  | 					prxy = proxy.New(args)
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 				for _, origin := range origins {
 | 
					
						
							| 
									
										
										
										
											2016-04-14 19:57:39 +01:00
										 |  |  | 					if t != nil {
 | 
					
						
							|  |  |  | 						z[origin].TransferTo = append(z[origin].TransferTo, t...)
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 					z[origin].NoReload = noReload
 | 
					
						
							| 
									
										
										
										
											2016-11-09 21:26:49 +00:00
										 |  |  | 					z[origin].Proxy = prxy
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-03-28 12:08:05 +01:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	return Zones{Z: z, Names: names}, nil
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | // TransferParse parses transfer statements: 'transfer to [address...]'.
 | 
					
						
							| 
									
										
										
										
											2016-10-02 19:41:00 +01:00
										 |  |  | func TransferParse(c *caddy.Controller, secondary bool) (tos, froms []string, err error) {
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 	what := c.Val()
 | 
					
						
							|  |  |  | 	if !c.NextArg() {
 | 
					
						
							| 
									
										
										
										
											2016-04-14 19:57:39 +01:00
										 |  |  | 		return nil, nil, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	value := c.Val()
 | 
					
						
							|  |  |  | 	switch what {
 | 
					
						
							|  |  |  | 	case "transfer":
 | 
					
						
							|  |  |  | 		if value == "to" {
 | 
					
						
							| 
									
										
										
										
											2016-04-15 17:08:31 +01:00
										 |  |  | 			tos = c.RemainingArgs()
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 			for i := range tos {
 | 
					
						
							| 
									
										
										
										
											2016-04-14 19:57:39 +01:00
										 |  |  | 				if tos[i] != "*" {
 | 
					
						
							| 
									
										
										
										
											2016-04-15 17:08:31 +01:00
										 |  |  | 					if x := net.ParseIP(tos[i]); x == nil {
 | 
					
						
							|  |  |  | 						return nil, nil, fmt.Errorf("must specify an IP addres: `%s'", tos[i])
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2016-04-14 19:57:39 +01:00
										 |  |  | 					tos[i] = middleware.Addr(tos[i]).Normalize()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 16:08:10 +01:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		if value == "from" {
 | 
					
						
							| 
									
										
										
										
											2016-10-02 19:41:00 +01:00
										 |  |  | 			if !secondary {
 | 
					
						
							|  |  |  | 				return nil, nil, fmt.Errorf("can't use `transfer from` when not being a seconary")
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-04-15 17:08:31 +01:00
										 |  |  | 			froms = c.RemainingArgs()
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 			for i := range froms {
 | 
					
						
							| 
									
										
										
										
											2016-04-14 19:57:39 +01:00
										 |  |  | 				if froms[i] != "*" {
 | 
					
						
							| 
									
										
										
										
											2016-04-15 17:08:31 +01:00
										 |  |  | 					if x := net.ParseIP(froms[i]); x == nil {
 | 
					
						
							|  |  |  | 						return nil, nil, fmt.Errorf("must specify an IP addres: `%s'", froms[i])
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2016-04-14 19:57:39 +01:00
										 |  |  | 					froms[i] = middleware.Addr(froms[i]).Normalize()
 | 
					
						
							|  |  |  | 				} else {
 | 
					
						
							|  |  |  | 					return nil, nil, fmt.Errorf("can't use '*' in transfer from")
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 16:08:10 +01:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return
 | 
					
						
							|  |  |  | }
 |