-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmdm_manage.go
148 lines (141 loc) · 4.51 KB
/
mdm_manage.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
package main
import (
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
)
// ManageHandler is the HTTP handler assosiated with the mdm management service. This is what constantly pushes configuration to the device.
// It is at the URL: /ManagementServer/MDM.svc
func ManageHandler(w http.ResponseWriter, r *http.Request) {
// Read The HTTP Request body
bodyRaw, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
body := string(bodyRaw)
// Retrieve the MessageID From The Body For The Response
// Note: The XML isn't parsed to keep this example simple but in your server it would have to have been
// So ignore the strings.Replace and Regex stuff you wouldn't do it this way
DeviceID := strings.Replace(strings.Replace(regexp.MustCompile(`<\/Target><Source><LocURI>[\s\S]*?<\/LocURI><\/Source>`).FindStringSubmatch(body)[0], "</Target><Source><LocURI>", "", -1), "</LocURI></Source>", "", -1)
// Retrieve the SessionID From The Body For The Response
// Note: The XML isn't parsed to keep this example simple but in your server it would have to have been
// So ignore the strings.Replace and Regex stuff you wouldn't do it this way
SessionID := strings.Replace(strings.Replace(regexp.MustCompile(`<SessionID>[\s\S]*?<\/SessionID>`).FindStringSubmatch(body)[0], "<SessionID>", "", -1), "</SessionID>", "", -1)
// Retrieve the MsgID From The Body For The Response
// Note: The XML isn't parsed to keep this example simple but in your server it would have to have been
// So ignore the strings.Replace and Regex stuff you wouldn't do it this way
MsgID := strings.Replace(strings.Replace(regexp.MustCompile(`<MsgID>[\s\S]*?<\/MsgID>`).FindStringSubmatch(body)[0], "<MsgID>", "", -1), "</MsgID>", "", -1)
// Create response payload
// A different response is need for AD so this is used to detect AD. This would be done by XML parsing the code not this for a production server.
// This is done this way to keep the server simple and easy to understand for non Go Lang developers.
var response string
if strings.Contains(body, "com.microsoft/MDM/AADUserToken") {
response = `<?xml version="1.0" encoding="UTF-8"?>
<SyncML xmlns="SYNCML:SYNCML1.2">
<SyncHdr>
<VerDTD>1.2</VerDTD>
<VerProto>DM/1.2</VerProto>
<SessionID>` + SessionID + `</SessionID>
<MsgID>` + MsgID + `</MsgID>
<Target>
<LocURI>` + DeviceID + `</LocURI>
</Target>
<Source>
<LocURI>https://` + domain + `/ManagementServer/MDM.svc</LocURI>
</Source>
</SyncHdr>
<SyncBody>
<Status>
<CmdID>1</CmdID>
<MsgRef>` + MsgID + `</MsgRef>
<CmdRef>0</CmdRef>
<Cmd>SyncHdr</Cmd>
<Data>200</Data>
</Status>
<Status>
<CmdID>2</CmdID>
<MsgRef>` + MsgID + `</MsgRef>
<CmdRef>2</CmdRef>
<Cmd>Alert</Cmd>
<Data>200</Data>
</Status>
<Status>
<CmdID>3</CmdID>
<MsgRef>` + MsgID + `</MsgRef>
<CmdRef>3</CmdRef>
<Cmd>Alert</Cmd>
<Data>200</Data>
</Status>
<Status>
<CmdID>4</CmdID>
<MsgRef>` + MsgID + `</MsgRef>
<CmdRef>4</CmdRef>
<Cmd>Alert</Cmd>
<Data>200</Data>
</Status>
<Status>
<CmdID>5</CmdID>
<MsgRef>` + MsgID + `</MsgRef>
<CmdRef>5</CmdRef>
<Cmd>Replace</Cmd>
<Data>200</Data>
</Status>
<Final />
</SyncBody>
</SyncML>`
} else {
response = `<?xml version="1.0" encoding="UTF-8"?>
<SyncML xmlns="SYNCML:SYNCML1.2">
<SyncHdr>
<VerDTD>1.2</VerDTD>
<VerProto>DM/1.2</VerProto>
<SessionID>` + SessionID + `</SessionID>
<MsgID>` + MsgID + `</MsgID>
<Target>
<LocURI>` + DeviceID + `</LocURI>
</Target>
<Source>
<LocURI>https://` + domain + `/ManagementServer/MDM.svc</LocURI>
</Source>
</SyncHdr>
<SyncBody>
<Status>
<CmdID>1</CmdID>
<MsgRef>` + MsgID + `</MsgRef>
<CmdRef>0</CmdRef>
<Cmd>SyncHdr</Cmd>
<Data>200</Data>
</Status>
<Status>
<CmdID>2</CmdID>
<MsgRef>` + MsgID + `</MsgRef>
<CmdRef>2</CmdRef>
<Cmd>Alert</Cmd>
<Data>200</Data>
</Status>
<Status>
<CmdID>3</CmdID>
<MsgRef>` + MsgID + `</MsgRef>
<CmdRef>3</CmdRef>
<Cmd>Alert</Cmd>
<Data>200</Data>
</Status>
<Status>
<CmdID>4</CmdID>
<MsgRef>` + MsgID + `</MsgRef>
<CmdRef>4</CmdRef>
<Cmd>Replace</Cmd>
<Data>200</Data>
</Status>
<Final />
</SyncBody>
</SyncML>`
}
// Return request body
responseRaw := []byte(strings.ReplaceAll(strings.ReplaceAll(response, "\n", ""), "\t", ""))
w.Header().Set("Content-Type", "application/vnd.syncml.dm+xml")
w.Header().Set("Content-Length", strconv.Itoa(len(responseRaw)))
w.Write(responseRaw)
}