-
Notifications
You must be signed in to change notification settings - Fork 2
/
mobile_config.py
96 lines (84 loc) · 3.4 KB
/
mobile_config.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
#!/usr/bin/env python
# -*- coding: utf8 -*-
#
# $Id$
#
# Copyright (c) 2012-2014 "dark[-at-]gotohack.org"
#
# This file is part of pymobiledevice2
#
# pymobiledevice2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
import logging
import plistlib
from pymobiledevice2.util import read_file
from pymobiledevice2.lockdown import LockdownClient
from optparse import OptionParser
from pprint import pprint
class MobileConfigService(object):
def __init__(self, lockdown, udid=None, logger=None):
self.logger = logger or logging.getLogger(__name__)
self.lockdown = lockdown if lockdown else LockdownClient(udid=udid)
self.service = lockdown.startService("com.apple.mobile.MCInstall")
def GetProfileList(self):
self.service.sendPlist({"RequestType":"GetProfileList"})
res = self.service.recvPlist()
if res.get("Status") != "Acknowledged":
self.logger.error("GetProfileList error")
self.logger.error(res)
return
return res
def InstallProfile(self, s):
#s = plistlib.writePlistToString(payload)
self.service.sendPlist({"RequestType":"InstallProfile", "Payload": plistlib.Data(s)})
return self.service.recvPlist()
def RemoveProfile(self, ident):
profiles = self.GetProfileList()
if not profiles:
return
if not profiles["ProfileMetadata"].has_key(ident):
self.logger.info("Trying to remove not installed profile %s", ident)
return
meta = profiles["ProfileMetadata"][ident]
pprint(meta)
data = plistlib.writePlistToString({"PayloadType": "Configuration",
"PayloadIdentifier": ident,
"PayloadUUID": meta["PayloadUUID"],
"PayloadVersion": meta["PayloadVersion"]
})
self.service.sendPlist({"RequestType":"RemoveProfile", "ProfileIdentifier": plistlib.Data(data)})
return self.service.recvPlist()
def main():
parser = OptionParser(usage="%prog")
parser.add_option("-l", "--list", dest="list", action="store_true",
default=False, help="List installed profiles")
parser.add_option("-i", "--install", dest="install", action="store",
metavar="FILE", help="Install profile")
parser.add_option("-r", "--remove", dest="remove", action="store",
metavar="IDENTIFIER", help="Remove profile")
(options, args) = parser.parse_args()
if not options.list and not options.install and not options.remove:
parser.print_help()
return
lockdown = LockdownClient()
mc = MobileConfigService(lockdown)
if options.list:
pprint(mc.GetProfileList())
elif options.install:
mc.InstallProfile(read_file(options.install))
elif options.remove:
pprint(mc.RemoveProfile(options.remove))
if __name__ == "__main__":
main()