From c438b740fe140d99264f3b1d8e2cac25697329f6 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Wed, 6 Dec 2017 22:03:54 +0000 Subject: [PATCH] Allow $INCLUDE to reference relative file (#598) When using a relative file in an $INCLUDE the file is referenced from the cwd from the calling processes; this changes it to be down from the view point where the file exists. Code from https://github.com/miekg/dns/issues/537#issuecomment-342932962 Fixes #537 --- scan.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/scan.go b/scan.go index 9b045ba7..82a5da0e 100644 --- a/scan.go +++ b/scan.go @@ -1,8 +1,10 @@ package dns import ( + "fmt" "io" "os" + "path/filepath" "strconv" "strings" ) @@ -297,16 +299,24 @@ func parseZone(r io.Reader, origin, f string, defttl *ttlState, t chan *Token, i return } // Start with the new file + includePath := l.token + if !filepath.IsAbs(includePath) { + includePath = filepath.Join(filepath.Dir(f), includePath) + } r1, e1 := os.Open(l.token) if e1 != nil { - t <- &Token{Error: &ParseError{f, "failed to open `" + l.token + "'", l}} + msg := fmt.Sprintf("failed to open `%s'", l.token) + if !filepath.IsAbs(l.token) { + msg += fmt.Sprintf(" as `%s'", includePath) + } + t <- &Token{Error: &ParseError{f, msg, l}} return } if include+1 > 7 { t <- &Token{Error: &ParseError{f, "too deeply nested $INCLUDE", l}} return } - parseZone(r1, neworigin, l.token, defttl, t, include+1) + parseZone(r1, neworigin, includePath, defttl, t, include+1) st = zExpectOwnerDir case zExpectDirTTLBl: if l.value != zBlank {