-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
185 lines (160 loc) · 7.32 KB
/
api.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
print "Content-type: text/html\n"
print ""
import cgi,cgitb
import requests
import json
import uuid
import base64
import plistlib
import subprocess
import os
"""
SETTINGS
"""
hostname='https://example.com/'
apiKey="---the apiKey---"
tokenLoc='/path/to/sToken_for_micromdm.vpptoken'
profileFolder='/path/to/MobileConfigs/'
munkiFolder='/path/to/munkirepo'
TEST_VALUES = {
"seriennummer": "C0XXXXXXXXXX",
"appid": "409183694",
"action": "installApp",
#"removeProfile" - "installProfile" - "installApp" - "listAllApps"
"profile": "Dock.mobileconfig",
# tom.dock.left - Dock.mobileconfig
}
iOS_list=["C1XXXXXXXXXX", "C2XXXXXXXXXX"]
"""
FUNCTIONS
"""
def getUdidForSerial(serial, hostname, apiKey):
response = requests.post(
hostname+"v1/devices",
headers={'Content-Type': 'application/json',},
auth=('micromdm', apiKey),
data=json.dumps({"filter_serial": [serial] })
)
return json.loads(response.content)["devices"][0]["udid"]
def getPricing():
pricing=json.loads(requests.get(vPPServiceConfigSrv["getVPPAssetsSrvUrl"], data=json.dumps({"sToken": sToken})).content)["assets"]
for aprice in pricing:
if ( appid == aprice["adamIdStr"] ) :
return aprice["pricingParam"]
def serialAssociatedToDevice(appid, seriennummer):
# ist die App schon der Seriennummer zugewiesen?
try:
licenses=json.loads(requests.get(vPPServiceConfigSrv["getLicensesSrvUrl"], data=json.dumps({"sToken": sToken,"serialNumber": seriennummer})).content)["licenses"]
for license in licenses:
if ( appid == license["adamIdStr"] ) :
return 1
return 0
except:
return 0
def getNameOfApp(appid) :
appInfo=requests.get("https://uclient-api.itunes.apple.com/WebObjects/MZStorePlatform.woa/wa/lookup?version=2&id="+appid+"&p=mdm-lockup&caller=MDM&platform=enterprisestore&cc=at&l=de").content
try:
return json.loads(appInfo)["results"][appid]["name"]
except:
return ""
def postMdmCommand(data) :
response = requests.post( hostname+"v1/commands" , headers={ 'Content-Type': 'application/json', }, auth=('micromdm', apiKey), data=data)
def readFile(fileLocation):
try:
file = open(fileLocation, "r")
fileContent= file.read()
file.close()
return fileContent
except Exception, e:
print "ERROR: Could not read " + fileLocation
def test_valid_request(manifest, package, search_keys) :
if manifest not in iOS_list:
this_manifest=plistlib.readPlist( munkiFolder+"/manifests/"+manifest)
for akey in search_keys:
if akey in this_manifest:
if package in this_manifest[akey]:
return True
if 'included_manifests' in this_manifest:
for a_manifest in this_manifest["included_manifests"]:
if test_valid_request(a_manifest, package, search_keys) == True:
return True
return False
else:
return True
def readPlist(plist_path):
try:
plist = subprocess.check_output( ['openssl', 'smime', '-in', plist_path, '-inform', 'der', '-verify', '-noverify'],stderr=open('/dev/null', 'w'))
return plistlib.readPlistFromString(plist)
except:
return plistlib.readPlist(plist_path)
"""
CGI PARSING
"""
cgitb.enable() #for debugging
form = cgi.FieldStorage()
seriennummer = form.getfirst('seriennummer', TEST_VALUES["seriennummer"])
appid = form.getfirst('appid', TEST_VALUES["appid"])
action = form.getfirst('action', TEST_VALUES["action"])
profile = form.getfirst('profile', TEST_VALUES["profile"])
"""
PREPARE
"""
sToken = readFile(tokenLoc)
# Die korrekten URLs fuer die Aufrufe holen
vPPServiceConfigSrv = json.loads(requests.get('https://vpp.itunes.apple.com/WebObjects/MZFinance.woa/wa/VPPServiceConfigSrv').content)
# Einen context
context = (json.loads(json.loads(requests.get(vPPServiceConfigSrv["clientConfigSrvUrl"], data=json.dumps({"sToken": sToken})).content)["clientContext"])["hostname"])
if context != hostname:
print 'context neu holen'
data = json.dumps({"sToken": sToken, "clientContext": "{\"hostname\": \"" + hostname + "\", \"guid\": \"" + str(uuid.uuid4()) + "\"}"})
context = requests.get(vPPServiceConfigSrv["clientConfigSrvUrl"], data=data).content
"""
DIFFERENT ACTIONS
"""
if ( action == "installApp" ) :
associated = serialAssociatedToDevice(appid, seriennummer)
myPricing = getPricing()
# Die App der Seriennummer zuweisen
if ( associated == 0 ) :
requests.get(vPPServiceConfigSrv["manageVPPLicensesByAdamIdSrvUrl"], data=json.dumps({"sToken": sToken,"adamIdStr": appid,"pricingParam": myPricing,"associateSerialNumbers": [ seriennummer ] }))
udid= getUdidForSerial (seriennummer, hostname, apiKey)
if test_valid_request(seriennummer, getNameOfApp(appid), ['managed_installs','optional_installs']):
if ( "udid" != "" ) :
postMdmCommand(json.dumps({"udid": udid, "request_type": "InstallApplication", "itunes_store_id": int(appid) , "options": {"purchase_method": 1}}))
else:
print "The App " + getNameOfApp(appid) + " is not allowed for " + seriennummer
if ( action == "removeApp" ) :
associated = serialAssociatedToDevice(appid, seriennummer)
myPricing = getPricing()
# Die App der Seriennummer wegnehmen
if ( associated == 1 ) :
requests.get(vPPServiceConfigSrv["manageVPPLicensesByAdamIdSrvUrl"], data=json.dumps({"sToken": sToken,"adamIdStr": appid,"pricingParam": myPricing,"disassociateSerialNumbers": [ seriennummer ] }))
if ( action == "listAllApps" ) :
assets=json.loads(requests.get(vPPServiceConfigSrv["getVPPAssetsSrvUrl"], data=json.dumps({"sToken": sToken})).content)["assets"]
for asset in assets :
appid = asset["adamIdStr"]
titel = getNameOfApp(appid)
print "ID: " + appid + " Titel: " + titel.encode('ascii', 'ignore').decode('ascii')
if ( action == "listApps4Serial" ) :
licenses=json.loads(requests.get(vPPServiceConfigSrv["getLicensesSrvUrl"], data=json.dumps({"sToken": sToken,"serialNumber": seriennummer})).content)["licenses"]
for license in licenses:
appid = license["adamIdStr"]
titel = getNameOfApp(appid)
print "ID: " + appid + " Titel: " + titel
if ( action == "installProfile" ) :
if (test_valid_request(seriennummer, "Profile - "+profile, ['managed_installs','optional_installs'])) or ( profile == "enroll") :
udid= getUdidForSerial (seriennummer, hostname, apiKey)
profile = base64.b64encode( readFile(profileFolder + profile + ".mobileconfig" ) )
postMdmCommand(json.dumps({"request_type": "InstallProfile", "udid": udid, "payload": profile}))
else:
print "The Profile " + profile + " is not allowed for " + seriennummer
if ( action == "removeProfile" ) :
the_profile=readPlist(profileFolder + profile + ".mobileconfig" )
identifier=the_profile['PayloadIdentifier']
if test_valid_request(seriennummer, "Profile - "+profile, ['managed_uninstalls','optional_installs']):
udid= getUdidForSerial (seriennummer, hostname, apiKey)
postMdmCommand(json.dumps({"request_type": "RemoveProfile", "udid": udid, "identifier": identifier}))
else:
print "The Profile " + profile + " can not be removed from " + seriennummer