forked from botherder/go-autoruns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoruns_darwin.go
114 lines (97 loc) · 2.62 KB
/
autoruns_darwin.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
109
110
111
112
113
114
package autoruns
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/botherder/go-files"
"howett.net/plist"
)
type Plist struct {
Label string `plist:"Label"`
ProgramArguments []string `plist:"ProgramArguments"`
RunAtLoad bool `plist:"RunAtLoad"`
}
func parsePlists(entryType string, folders []string) (records []*Autorun) {
for _, folder := range folders {
// Check if the folders exists.
if _, err := os.Stat(folder); os.IsNotExist(err) {
continue
}
// Get list of files in folder.
filesList, err := ioutil.ReadDir(folder)
if err != nil {
continue
}
// Loop through all files in folder.
for _, fileEntry := range filesList {
// Open the plist file.
filePath := filepath.Join(folder, fileEntry.Name())
reader, err := os.Open(filePath)
if err != nil {
continue
}
defer reader.Close()
// Parse the plist file.
var p Plist
decoder := plist.NewDecoder(reader)
err = decoder.Decode(&p)
if err != nil {
continue
}
if len(p.ProgramArguments) == 0 {
continue
}
// We skip those that do not start automatically.
if !p.RunAtLoad {
continue
}
imagePath := p.ProgramArguments[0]
arguments := ""
if len(p.ProgramArguments) > 1 {
arguments = strings.Join(p.ProgramArguments[1:], " ")
}
md5, _ := files.HashFile(imagePath, "md5")
sha1, _ := files.HashFile(imagePath, "sha1")
sha256, _ := files.HashFile(imagePath, "sha256")
newAutorun := Autorun{
Type: entryType,
Location: filePath,
ImagePath: imagePath,
ImageName: filepath.Base(imagePath),
Arguments: arguments,
MD5: md5,
SHA1: sha1,
SHA256: sha256,
LaunchString: imagePath,
}
if arguments != "" {
newAutorun.LaunchString += " " + arguments
}
// Add new record to list.
records = append(records, &newAutorun)
}
}
return
}
// This function just invokes all the platform-dependant functions.
func getAutoruns() (records []*Autorun) {
// Startup and run as root.
launchDaemons := []string{
"/Library/LaunchDaemons",
"/System/Library/LaunchDaemons",
}
// Launch when any user logs in.
launchAgents := []string{
"/Library/LaunchAgents",
"/System/Library/LaunchAgents",
}
// Launch when current user logs in.
launchAgentsUser := []string{
filepath.Join(os.Getenv("HOME"), "Library", "LaunchAgents"),
}
records = append(records, parsePlists("launch_daemons", launchDaemons)...)
records = append(records, parsePlists("launch_agents", launchAgents)...)
records = append(records, parsePlists("launch_agents_user", launchAgentsUser)...)
return
}