forked from botherder/go-autoruns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoruns_freebsd.go
108 lines (91 loc) · 2.02 KB
/
autoruns_freebsd.go
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
98
99
100
101
102
103
104
105
106
107
108
package autoruns
import (
"bufio"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
var enabledServices []string
func parseRCConf() {
file, err := os.Open("/etc/rc.conf")
if err != nil {
return
}
defer file.Close()
rxp, err := regexp.Compile("^(\\w+)_enable=\"YES\"$")
if err != nil {
return
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
matches := rxp.FindStringSubmatch(line)
if len(matches) < 2 {
continue
}
serviceName := matches[1]
if serviceName == "" {
continue
}
enabledServices = append(enabledServices, serviceName)
}
if err := scanner.Err(); err != nil {
return
}
}
func parseRCScripts(entryType, folder string) (records []*Autorun) {
// Check if the folders exists.
if _, err := os.Stat(folder); os.IsNotExist(err) {
return
}
// Get list of files in folder.
filesList, err := ioutil.ReadDir(folder)
if err != nil {
return
}
rxp, err := regexp.Compile("^name=(\\w+)$")
// Loop through all files in folder.
for _, fileEntry := range filesList {
filePath := filepath.Join(folder, fileEntry.Name())
file, err := os.Open(filePath)
if err != nil {
continue
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
line = strings.Replace(line, "\"", "", -1)
matches := rxp.FindStringSubmatch(line)
if len(matches) < 2 {
continue
}
serviceName := matches[1]
if serviceName == "" {
continue
}
for _, enabled := range enabledServices {
if enabled == serviceName {
newAutorun := Autorun{
Type: entryType,
Location: filePath,
ImageName: serviceName,
}
records = append(records, &newAutorun)
break
}
}
}
}
return
}
func getAutoruns() (records []*Autorun) {
parseRCConf()
records = append(records, parseRCScripts("rc.d", "/etc/rc.d/")...)
records = append(records, parseRCScripts("local_rc.d", "/usr/local/etc/rc.d/")...)
return
}