summaryrefslogtreecommitdiff
path: root/unix.go
blob: 722ec82bdc5be5acc64d3ad41e80104c9a2a9540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Various Linux/Unix'y things

// https://wiki.archlinux.org/title/Dynamic_DNS

package main

import 	(
	"log"
	"os"
	"os/exec"
	"net"
	"bytes"
	"fmt"
	"strings"

	"go.wit.com/shell"
)

func CheckSuperuser() bool {
	return os.Getuid() == 0
}

func Escalate() {
	if os.Getuid() != 0 {
		cmd := exec.Command("sudo", "./control-panel-dns") // TODO: get the actual path
		cmd.Stdin = os.Stdin
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		err := cmd.Run()
		if err != nil {
			debug(LogError, "exit in Escalate()")
			exit(err)
		}
	}
}

// You need permission to do a zone transfer. Otherwise:
// dig +noall +answer +multiline lab.wit.com any 
// dig +all +multiline fire.lab.wit.com # gives the zonefile header (ttl vals)
func DumpPublicDNSZone(zone string) {
	entries, err := net.LookupHost(zone)
	if err != nil {
		panic(err)
	}
	for _, entry := range entries {
		log.Println(entry)
	}
}

func dumpIPs(host string) {
    ips, err := net.LookupIP(host)
        if err != nil {
		debug(LogError, "dumpIPs() failed:", err)
        }
        for _, ip := range ips {
                log.Println(host, ip)
        }
}

/*
	check if ddclient is installed, working, and/or configured
	https://github.com/ddclient/ddclient
*/
func ddclient() {
}

/*
	check if ddupdate is installed, working, and/or configured
*/
func ddupdate() {
}

func run(s string) string {
	cmdArgs := strings.Fields(s)
	// Define the command you want to run
	// cmd := exec.Command(cmdArgs)
	cmd := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)

	// Create a buffer to capture the output
	var out bytes.Buffer

	// Set the output of the command to the buffer
	cmd.Stdout = &out

	// Run the command
	err := cmd.Run()
	if err != nil {
		fmt.Println("Error running command:", err)
		return ""
	}

	tmp := shell.Chomp(out.String())
	// Output the results
	debug(LogExec, "Command Output:", tmp)

	return tmp
}