forked from micromdm/micromdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_app.go
204 lines (176 loc) · 4.7 KB
/
apply_app.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"bytes"
"compress/zlib"
"context"
"encoding/binary"
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"github.com/groob/plist"
"github.com/micromdm/micromdm/appmanifest"
"github.com/pkg/errors"
)
func (cmd *applyCommand) applyApp(args []string) error {
flagset := flag.NewFlagSet("app", flag.ExitOnError)
var (
flPkgPath = flagset.String("pkg", "", "path to a distribution pkg.")
flPkgURL = flagset.String("pkg-url", "", "use custom pkg url")
flAppManifest = flagset.String("manifest", "-", `path to an app manifest. optional,
will be created if file does not exist.`)
flHashSize = flagset.Int64("md5size", appmanifest.DefaultMD5Size, "md5 hash size in bytes (optional)")
flSign = flagset.String("sign", "", "sign package before importing, requires specifying a product ID (optional)")
flUpload = flagset.Bool("upload", false, "upload package and/or manifest to micromdm repository.")
)
flagset.Usage = usageFor(flagset, "mdmctl apply app [flags]")
if err := flagset.Parse(args); err != nil {
return err
}
pkgurl := *flPkgURL
if pkgurl == "" {
su, err := cmd.serverRepoURL()
if err != nil {
return err
}
pkgurl = pkgURL(su, *flPkgPath)
}
pkg := *flPkgPath
signed, err := checkSignature(pkg)
if err != nil {
return err
}
distribution, err := checkDistribution(*flPkgPath)
if err != nil {
return err
}
if !distribution {
fmt.Println(`
[WARNING] The package you're importing is not a macOS distribution package. MDM requires distribution packages.
You can turn a flat package to a distribution one with the productbuild command:
productbuild --package someFlatPkg.pkg myNewPkg.pkg
Please rebuild the package and re-run the command.
`)
}
if !signed {
if *flSign == "" {
flagset.Usage()
return errors.New(`MDM packages must be signed. Provide signed package or Developer ID with -sign flag`)
}
outpath := filepath.Join(os.TempDir(), filepath.Base(*flPkgPath))
if err := signPackage(*flPkgPath, outpath, *flSign); err != nil {
return err
}
pkg = outpath // use signed package to create the manifest
}
// open pkg file
f, err := os.Open(pkg)
if err != nil {
return err
}
defer f.Close()
opts := []appmanifest.Option{appmanifest.WithMD5Size(*flHashSize)}
manifest, err := appmanifest.Create(&appFile{f}, pkgurl, opts...)
if err != nil {
return errors.Wrap(err, "creating manifest")
}
var buf bytes.Buffer
enc := plist.NewEncoder(&buf)
enc.Indent(" ")
if err := enc.Encode(manifest); err != nil {
return err
}
if *flUpload {
// we read the file to generate the appmanifest, so we need to seek to the beginning of the file again.
if _, err := f.Seek(0, 0); err != nil {
return errors.Wrap(err, "reset pkg file reader")
}
err := cmd.applysvc.UploadApp(context.TODO(), nameMannifest(f.Name()), &buf, filepath.Base(f.Name()), f)
if err != nil {
return err
}
}
switch *flAppManifest {
case "":
case "-":
_, err := os.Stdout.Write(buf.Bytes())
return err
default:
return ioutil.WriteFile(*flAppManifest, buf.Bytes(), 0644)
}
return nil
}
func nameMannifest(pkgName string) string {
trimmed := strings.TrimSuffix(filepath.Base(pkgName), filepath.Ext(pkgName))
return trimmed + ".plist"
}
func checkDistribution(pkgPath string) (bool, error) {
const (
xarHeaderMagic = 0x78617221
xarHeaderSize = 28
)
f, err := os.Open(pkgPath)
if err != nil {
return false, err
}
defer f.Close()
hdr := make([]byte, xarHeaderSize)
_, err = f.ReadAt(hdr, 0)
if err != nil {
return false, err
}
tocLenZlib := binary.BigEndian.Uint64(hdr[8:16])
ztoc := make([]byte, tocLenZlib)
_, err = f.ReadAt(ztoc, xarHeaderSize)
if err != nil {
return false, err
}
br := bytes.NewBuffer(ztoc)
zr, err := zlib.NewReader(br)
if err != nil {
return false, err
}
toc, err := ioutil.ReadAll(zr)
if err != nil {
return false, err
}
return bytes.Contains(toc, []byte(`<name>Distribution</name>`)), nil
}
func (cmd *applyCommand) serverRepoURL() (string, error) {
return repoURL(cmd.config.ServerURL)
}
func pkgURL(repoURL, pkgPath string) string {
u, _ := url.Parse(repoURL)
newPath := path.Join(u.Path, filepath.Base(pkgPath))
u.Path = newPath
return u.String()
}
func repoURL(server string) (string, error) {
serverURL, err := url.Parse(server)
if err != nil {
return "", err
}
serverURL.Path = "/repo"
return serverURL.String(), nil
}
// replaces .pkg with .plist
func manifestURL(repoURL, pkgPath string) string {
pu := pkgURL(repoURL, pkgPath)
trimmed := strings.TrimSuffix(pu, path.Ext(pu))
return trimmed + ".plist"
}
type appFile struct {
*os.File
}
func (af *appFile) Size() int64 {
info, err := af.Stat()
if err != nil {
log.Fatal(err)
}
return info.Size()
}