forked from micromdm/micromdm
/
service.go
328 lines (274 loc) · 9.44 KB
/
service.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package enroll
import (
"bytes"
"crypto/x509"
"io/ioutil"
"log"
"strings"
"sync"
"github.com/groob/plist"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/micromdm/micromdm/platform/config"
"github.com/micromdm/micromdm/platform/profile"
"github.com/micromdm/micromdm/platform/pubsub"
)
const (
EnrollmentProfileId string = "com.github.micromdm.micromdm.enroll"
OTAProfileId string = "com.github.micromdm.micromdm.ota"
)
type Service interface {
Enroll(ctx context.Context) (profile.Mobileconfig, error)
OTAEnroll(ctx context.Context) (profile.Mobileconfig, error)
OTAPhase2(ctx context.Context) (profile.Mobileconfig, error)
OTAPhase3(ctx context.Context) (profile.Mobileconfig, error)
}
func NewService(topic TopicProvider, sub pubsub.Subscriber, caCertPath, scepURL, scepChallenge, url, tlsCertPath, scepSubject string, profileDB profile.Store) (Service, error) {
var caCert, tlsCert []byte
var err error
if caCertPath != "" {
caCert, err = ioutil.ReadFile(caCertPath)
if err != nil {
return nil, err
}
}
if tlsCertPath != "" {
tlsCert, err = ioutil.ReadFile(tlsCertPath)
if err != nil {
return nil, err
}
}
if scepSubject == "" {
scepSubject = "/O=MicroMDM/CN=MicroMDM Identity (%ComputerName%)"
}
subjectElements := strings.Split(scepSubject, "/")
var subject [][][]string
for _, element := range subjectElements {
if element == "" {
continue
}
subjectKeyValue := strings.Split(element, "=")
subject = append(subject, [][]string{[]string{subjectKeyValue[0], subjectKeyValue[1]}})
}
// fetch the push topic from the db.
// will be "" if the push certificate hasn't been uploaded yet
pushTopic, _ := topic.PushTopic()
svc := &service{
URL: url,
SCEPURL: scepURL,
SCEPSubject: subject,
SCEPChallenge: scepChallenge,
CACert: caCert,
TLSCert: tlsCert,
ProfileDB: profileDB,
Topic: pushTopic,
topicProvier: topic,
}
if err := updateTopic(svc, sub); err != nil {
return nil, errors.Wrap(err, "enroll: start topic update goroutine")
}
return svc, nil
}
func updateTopic(svc *service, sub pubsub.Subscriber) error {
configEvents, err := sub.Subscribe(context.TODO(), "enroll-server-configs", config.ConfigTopic)
if err != nil {
return errors.Wrap(err, "update enrollment service")
}
go func() {
for {
select {
case <-configEvents:
topic, err := svc.topicProvier.PushTopic()
if err != nil {
log.Printf("enroll: get push topic %s\n", topic)
}
svc.mu.Lock()
svc.Topic = topic
svc.mu.Unlock()
// terminate the loop here because the topic should never change
goto exit
}
}
exit:
return
}()
return nil
}
type service struct {
URL string
SCEPURL string
SCEPChallenge string
SCEPSubject [][][]string
CACert []byte
TLSCert []byte
ProfileDB profile.Store
topicProvier TopicProvider
mu sync.RWMutex
Topic string // APNS Topic for MDM notifications
}
type TopicProvider interface {
PushTopic() (string, error)
}
func profileOrPayloadFromFunc(f interface{}) (interface{}, error) {
fPayload, ok := f.(func() (Payload, error))
if !ok {
fProfile := f.(func() (Profile, error))
return fProfile()
}
return fPayload()
}
func profileOrPayloadToMobileconfig(in interface{}) (profile.Mobileconfig, error) {
if _, ok := in.(Payload); !ok {
_ = in.(Profile)
}
buf := new(bytes.Buffer)
enc := plist.NewEncoder(buf)
enc.Indent(" ")
err := enc.Encode(in)
return buf.Bytes(), err
}
func (svc *service) findOrMakeMobileconfig(id string, f interface{}) (profile.Mobileconfig, error) {
p, err := svc.ProfileDB.ProfileById(id)
if err != nil {
if profile.IsNotFound(err) {
profile, err := profileOrPayloadFromFunc(f)
if err != nil {
return nil, err
}
return profileOrPayloadToMobileconfig(profile)
}
return nil, err
}
return p.Mobileconfig, nil
}
func (svc *service) Enroll(ctx context.Context) (profile.Mobileconfig, error) {
return svc.findOrMakeMobileconfig(EnrollmentProfileId, svc.MakeEnrollmentProfile)
}
const perUserConnections = "com.apple.mdm.per-user-connections"
func (svc *service) MakeEnrollmentProfile() (Profile, error) {
profile := NewProfile()
profile.PayloadIdentifier = EnrollmentProfileId
profile.PayloadOrganization = "MicroMDM"
profile.PayloadDisplayName = "Enrollment Profile"
profile.PayloadDescription = "The server may alter your settings"
profile.PayloadScope = "System"
mdmPayload := NewPayload("com.apple.mdm")
mdmPayload.PayloadDescription = "Enrolls with the MDM server"
mdmPayload.PayloadOrganization = "MicroMDM"
mdmPayload.PayloadIdentifier = EnrollmentProfileId + ".mdm"
mdmPayload.PayloadScope = "System"
svc.mu.Lock()
topic := svc.Topic
svc.mu.Unlock()
mdmPayloadContent := MDMPayloadContent{
Payload: *mdmPayload,
AccessRights: allRights(),
CheckInURL: svc.URL + "/mdm/checkin",
CheckOutWhenRemoved: true,
ServerURL: svc.URL + "/mdm/connect",
Topic: topic,
SignMessage: true,
ServerCapabilities: []string{perUserConnections},
}
payloadContent := []interface{}{}
if svc.SCEPURL != "" {
scepContent := SCEPPayloadContent{
URL: svc.SCEPURL,
Keysize: 2048,
KeyType: "RSA",
KeyUsage: int(x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment),
Name: "Device Management Identity Certificate",
Subject: svc.SCEPSubject,
}
if svc.SCEPChallenge != "" {
scepContent.Challenge = svc.SCEPChallenge
}
scepPayload := NewPayload("com.apple.security.scep")
scepPayload.PayloadDescription = "Configures SCEP"
scepPayload.PayloadDisplayName = "SCEP"
scepPayload.PayloadIdentifier = EnrollmentProfileId + ".scep"
scepPayload.PayloadOrganization = "MicroMDM"
scepPayload.PayloadContent = scepContent
scepPayload.PayloadScope = "System"
payloadContent = append(payloadContent, *scepPayload)
mdmPayloadContent.IdentityCertificateUUID = scepPayload.PayloadUUID
}
payloadContent = append(payloadContent, mdmPayloadContent)
if len(svc.CACert) > 0 {
caPayload := NewPayload("com.apple.security.root")
caPayload.PayloadDisplayName = "Root certificate for MicroMDM"
caPayload.PayloadDescription = "Installs the root CA certificate for MicroMDM"
caPayload.PayloadIdentifier = EnrollmentProfileId + ".cert.ca"
caPayload.PayloadContent = svc.CACert
payloadContent = append(payloadContent, *caPayload)
}
// Client needs to trust us at this point if we are using a self signed certificate.
if len(svc.TLSCert) > 0 {
tlsPayload := NewPayload("com.apple.security.pem")
tlsPayload.PayloadDisplayName = "Self-signed TLS certificate for MicroMDM"
tlsPayload.PayloadDescription = "Installs the TLS certificate for MicroMDM"
tlsPayload.PayloadIdentifier = EnrollmentProfileId + ".cert.selfsigned"
tlsPayload.PayloadContent = svc.TLSCert
payloadContent = append(payloadContent, *tlsPayload)
}
profile.PayloadContent = payloadContent
return *profile, nil
}
// OTAEnroll returns an Over-the-Air "Profile Service" Payload for enrollment.
func (svc *service) OTAEnroll(ctx context.Context) (profile.Mobileconfig, error) {
return svc.findOrMakeMobileconfig(OTAProfileId, svc.MakeOTAEnrollPayload)
}
func (svc *service) MakeOTAEnrollPayload() (Payload, error) {
payload := NewPayload("Profile Service")
payload.PayloadIdentifier = OTAProfileId
payload.PayloadDisplayName = "MicroMDM Profile Service"
payload.PayloadDescription = "Profile Service enrollment"
payload.PayloadOrganization = "MicroMDM"
payload.PayloadContent = ProfileServicePayload{
URL: svc.URL + "/ota/phase23",
Challenge: "",
DeviceAttributes: []string{"UDID", "VERSION", "PRODUCT", "SERIAL", "MEID", "IMEI"},
}
// yes, this is a bare Payload, not a Profile
return *payload, nil
}
// OTAPhase2 returns a SCEP Profile for use in phase 2 of Over-the-Air enrollment.
func (svc *service) OTAPhase2(ctx context.Context) (profile.Mobileconfig, error) {
return svc.findOrMakeMobileconfig(OTAProfileId+".phase2", svc.MakeOTAPhase2Profile)
}
func (svc *service) MakeOTAPhase2Profile() (Profile, error) {
profile := NewProfile()
profile.PayloadIdentifier = OTAProfileId + ".phase2"
profile.PayloadOrganization = "MicroMDM"
profile.PayloadDisplayName = "OTA Phase 2"
profile.PayloadDescription = "The server may alter your settings"
profile.PayloadScope = "System"
scepContent := SCEPPayloadContent{
URL: svc.SCEPURL,
Keysize: 2048, // NOTE: OTA docs recommend 1024
KeyType: "RSA",
KeyUsage: int(x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment),
Name: "OTA Phase 2 Certificate",
Subject: svc.SCEPSubject,
}
if svc.SCEPChallenge != "" {
scepContent.Challenge = svc.SCEPChallenge
}
scepPayload := NewPayload("com.apple.security.scep")
scepPayload.PayloadDescription = "Configures SCEP"
scepPayload.PayloadDisplayName = "SCEP"
scepPayload.PayloadIdentifier = OTAProfileId + ".phase2.scep"
scepPayload.PayloadOrganization = "MicroMDM"
scepPayload.PayloadContent = scepContent
scepPayload.PayloadScope = "System"
profile.PayloadContent = append(profile.PayloadContent, *scepPayload)
return *profile, nil
}
// OTAPhase3 returns a Profile for use in phase 3 of Over-the-Air profile enrollment.
// This would typically be the final or end profile of the Over-the-Air
// enrollment process. In our case this would probably be a device-specifc
// MDM enrollment payload.
// TODO: Not implemented.
func (svc *service) OTAPhase3(ctx context.Context) (profile.Mobileconfig, error) {
return profile.Mobileconfig{}, nil
}