1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

BIND: Automatically create the subdirectories leading up to zonefiles (#2397)

This commit is contained in:
Tom Limoncelli
2023-05-26 13:16:20 -04:00
committed by GitHub
parent 3492b26d0a
commit 207ed695cf
2 changed files with 30 additions and 3 deletions

View File

@@ -156,7 +156,7 @@ func (c *bindProvider) ListZones() ([]string, error) {
func (c *bindProvider) GetZoneRecords(domain string, meta map[string]string) (models.Records, error) {
if _, err := os.Stat(c.directory); os.IsNotExist(err) {
printer.Printf("\nWARNING: BIND directory %q does not exist!\n", c.directory)
printer.Printf("\nWARNING: BIND directory %q does not exist! (will create)\n", c.directory)
}
if c.zonefile == "" {
@@ -312,7 +312,11 @@ func (c *bindProvider) GetZoneRecordsCorrections(dc *models.DomainConfig, foundR
Msg: msg,
F: func() error {
printer.Printf("WRITING ZONEFILE: %v\n", c.zonefile)
zf, err := os.Create(c.zonefile)
fname, err := preprocessFilename(c.zonefile)
if err != nil {
return fmt.Errorf("could not create zonefile: %w", err)
}
zf, err := os.Create(fname)
if err != nil {
return fmt.Errorf("could not create zonefile: %w", err)
}
@@ -335,3 +339,21 @@ func (c *bindProvider) GetZoneRecordsCorrections(dc *models.DomainConfig, foundR
return corrections, nil
}
// preprocessFilename pre-processes a filename we're about to os.Create()
// * On Windows systems, it translates the seperator.
// * It attempts to mkdir the directories leading up to the filename.
// * If running on Linux as root, it does not attempt to create directories.
func preprocessFilename(name string) (string, error) {
universalName := filepath.FromSlash(name)
// Running as root? Don't create the parent directories. It is unsafe.
if os.Getuid() != 0 {
// Create the parent directories
dir := filepath.Dir(name)
universalDir := filepath.FromSlash(dir)
if err := os.MkdirAll(universalDir, 0750); err != nil {
return "", err
}
}
return universalName, nil
}