dnscontrol/providers/activedir/getzones_windows.go

85 lines
2.1 KiB
Go
Raw Normal View History

2016-08-23 08:31:50 +08:00
package activedir
import (
"fmt"
"os/exec"
"strconv"
"strings"
"sync"
2016-08-23 08:31:50 +08:00
)
var checkPS sync.Once
var psAvailible = false
2020-10-30 01:36:47 +08:00
func (c *activedirProvider) getRecords(domainname string) ([]byte, error) {
2016-08-23 08:31:50 +08:00
// If we are using PowerShell, make sure it is enabled
// and then run the PS1 command to generate the adzonedump file.
2016-08-23 08:31:50 +08:00
if !c.fake {
checkPS.Do(func() {
psAvailible = c.isPowerShellReady()
if !psAvailible {
fmt.Printf("\n\n\n")
fmt.Printf("***********************************************\n")
fmt.Printf("PowerShell DnsServer module not installed.\n")
fmt.Printf("See http://social.technet.microsoft.com/wiki/contents/articles/2202.remote-server-administration-tools-rsat-for-windows-client-and-windows-server-dsforum2wiki.aspx\n")
fmt.Printf("***********************************************\n")
fmt.Printf("\n\n\n")
}
})
if !psAvailible {
return nil, fmt.Errorf("powershell module DnsServer not installed")
2016-08-23 08:31:50 +08:00
}
_, err := c.powerShellExec(c.generatePowerShellZoneDump(domainname), true)
2016-08-23 08:31:50 +08:00
if err != nil {
return []byte{}, err
}
}
// Return the contents of zone.*.json file instead.
return c.readZoneDump(domainname)
}
2020-10-30 01:36:47 +08:00
func (c *activedirProvider) isPowerShellReady() bool {
query, _ := c.powerShellExec(`(Get-Module -ListAvailable DnsServer) -ne $null`, true)
2016-08-23 08:31:50 +08:00
q, err := strconv.ParseBool(strings.TrimSpace(string(query)))
if err != nil {
return false
}
return q
}
2020-10-30 01:36:47 +08:00
func (c *activedirProvider) powerShellDoCommand(command string, shouldLog bool) error {
if c.fake {
2016-08-23 08:31:50 +08:00
// If fake, just record the command.
return c.powerShellRecord(command)
2016-08-23 08:31:50 +08:00
}
_, err := c.powerShellExec(command, shouldLog)
2016-08-23 08:31:50 +08:00
return err
}
2020-10-30 01:36:47 +08:00
func (c *activedirProvider) powerShellExec(command string, shouldLog bool) ([]byte, error) {
2016-08-23 08:31:50 +08:00
// log it.
err := c.logCommand(command)
2016-08-23 08:31:50 +08:00
if err != nil {
return nil, err
2016-08-23 08:31:50 +08:00
}
// Run it.
out, err := exec.Command("powershell", "-NoProfile", command).CombinedOutput()
if err != nil {
// If there was an error, log it.
c.logErr(err)
2016-08-23 08:31:50 +08:00
}
if shouldLog {
err = c.logOutput(string(out))
if err != nil {
return []byte{}, err
}
2016-08-23 08:31:50 +08:00
}
// Return the result.
return out, err
}