| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package log
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-05-31 20:28:53 +01:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"log"
 | 
					
						
							|  |  |  | 	"os"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware/pkg/response"
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func init() {
 | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("log", caddy.Plugin{
 | 
					
						
							|  |  |  | 		ServerType: "dns",
 | 
					
						
							|  |  |  | 		Action:     setup,
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	rules, err := logParse(c)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | 		return middleware.Error("log", err)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Open the log files for writing when the server starts
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	c.OnStartup(func() error {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		for i := 0; i < len(rules); i++ {
 | 
					
						
							| 
									
										
										
										
											2017-05-31 20:28:53 +01:00
										 |  |  | 			// We only support stdout
 | 
					
						
							|  |  |  | 			writer := os.Stdout
 | 
					
						
							|  |  |  | 			if rules[i].OutputFile != "stdout" {
 | 
					
						
							|  |  |  | 				return middleware.Error("log", fmt.Errorf("invalid log file: %s", rules[i].OutputFile))
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			rules[i].Log = log.New(writer, "", 0)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 11:26:00 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
 | 
					
						
							| 
									
										
										
										
											2016-08-20 23:03:36 +01:00
										 |  |  | 		return Logger{Next: next, Rules: rules, ErrorFunc: dnsserver.DefaultErrorFunc}
 | 
					
						
							| 
									
										
										
										
											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 logParse(c *caddy.Controller) ([]Rule, error) {
 | 
					
						
							|  |  |  | 	var rules []Rule
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if len(args) == 0 {
 | 
					
						
							|  |  |  | 			// Nothing specified; use defaults
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 			rules = append(rules, Rule{
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 				NameScope:  ".",
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 				OutputFile: DefaultLogFilename,
 | 
					
						
							|  |  |  | 				Format:     DefaultLogFormat,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			})
 | 
					
						
							|  |  |  | 		} else if len(args) == 1 {
 | 
					
						
							| 
									
										
										
										
											2017-05-31 20:28:53 +01:00
										 |  |  | 			// Only an output file specified.
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 			rules = append(rules, Rule{
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 				NameScope:  ".",
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 				OutputFile: args[0],
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 				Format:     DefaultLogFormat,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			})
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 			// Name scope, output file, and maybe a format specified
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 			format := DefaultLogFormat
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if len(args) > 2 {
 | 
					
						
							|  |  |  | 				switch args[2] {
 | 
					
						
							|  |  |  | 				case "{common}":
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 					format = CommonLogFormat
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 				case "{combined}":
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 					format = CombinedLogFormat
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 				default:
 | 
					
						
							|  |  |  | 					format = args[2]
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 			rules = append(rules, Rule{
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 				NameScope:  dns.Fqdn(args[0]),
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 				OutputFile: args[1],
 | 
					
						
							|  |  |  | 				Format:     format,
 | 
					
						
							|  |  |  | 			})
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Class refinements in an extra block.
 | 
					
						
							|  |  |  | 		for c.NextBlock() {
 | 
					
						
							|  |  |  | 			switch c.Val() {
 | 
					
						
							|  |  |  | 			// class followed by all, denial, error or success.
 | 
					
						
							|  |  |  | 			case "class":
 | 
					
						
							|  |  |  | 				classes := c.RemainingArgs()
 | 
					
						
							|  |  |  | 				if len(classes) == 0 {
 | 
					
						
							|  |  |  | 					return nil, c.ArgErr()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				cls, err := response.ClassFromString(classes[0])
 | 
					
						
							|  |  |  | 				if err != nil {
 | 
					
						
							|  |  |  | 					return nil, err
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				// update class and the last added Rule (bit icky)
 | 
					
						
							|  |  |  | 				rules[len(rules)-1].Class = cls
 | 
					
						
							|  |  |  | 			default:
 | 
					
						
							|  |  |  | 				return nil, c.ArgErr()
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return rules, nil
 | 
					
						
							|  |  |  | }
 |