forked from nmcspadden/SalProfileGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcxToProfile.py
executable file
·345 lines (286 loc) · 12.9 KB
/
mcxToProfile.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/python
# mcxToProfile.py
# Simple utility to assist with creating Custom Settings Configuration Profiles
# from plist files and Directory Services nodes
import sys
import os
import optparse
import subprocess
import re
from uuid import uuid4
try:
from FoundationPlist import *
except:
from plistlib import *
class PayloadDict:
"""Class to create and manipulate Configuration Profiles.
The actual plist content can be accessed as a dictionary via the 'data' attribute.
"""
def __init__(self, identifier, uuid=False, removal_allowed=False, organization='', displayname=''):
self.data = {}
self.data['PayloadVersion'] = 1
self.data['PayloadOrganization'] = organization
if uuid:
self.data['PayloadUUID'] = uuid
else:
self.data['PayloadUUID'] = makeNewUUID()
if removal_allowed:
self.data['PayloadRemovalDisallowed'] = False
else:
self.data['PayloadRemovalDisallowed'] = True
self.data['PayloadType'] = 'Configuration'
self.data['PayloadScope'] = 'System'
self.data['PayloadDescription'] = "Included custom settings:\n"
self.data['PayloadDisplayName'] = displayname
self.data['PayloadIdentifier'] = identifier
# store git commit for reference if possible
self.gitrev = None
root_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
if '.git' in os.listdir(root_dir):
git_p = subprocess.Popen('git rev-parse HEAD',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
cwd=root_dir)
out, err = git_p.communicate()
if not git_p.returncode:
self.gitrev = out.strip()
# An empty list for 'sub payloads' that we'll fill later
self.data['PayloadContent'] = []
def _addPayload(self, payload_content_dict):
"""Add a Custom Settings payload to the profile. Takes a dict which will be the
PayloadContent dict within the payload. Handles the boilerplate, naming and descriptive
elements.
"""
domains = payload_content_dict.keys()
if len(domains) == 1:
domain = domains[0]
self.data['PayloadDescription'] += "%s\n" % domain
else:
domain = 'multiple preference domains'
self.data['PayloadDescription'] += '\n'.join(domains)
payload_dict = {}
# Boilerplate
payload_dict['PayloadVersion'] = 1
payload_dict['PayloadUUID'] = makeNewUUID()
payload_dict['PayloadEnabled'] = True
payload_dict['PayloadType'] = 'com.apple.ManagedClient.preferences'
payload_dict['PayloadIdentifier'] = "%s.%s.alacarte.customsettings.%s" % (
'MCXToProfile', self.data['PayloadUUID'], payload_dict['PayloadUUID'])
# Update the top-level descriptive info
if self.data['PayloadDisplayName'] == '':
self.data['PayloadDisplayName'] = 'MCXToProfile: %s' % domain
# Add our actual MCX/Plist content
payload_dict['PayloadContent'] = payload_content_dict
# Add to the profile's PayloadContent array
self.data['PayloadContent'].append(payload_dict)
def addPayloadFromPlistContents(self, plist_dict, domain, manage, is_byhost=False):
"""Add one plist dict contents to the profile's payloads. domain is the
preferences domain (ie. com.apple.finder), manage is one of 'Once', 'Often' or 'Always',
and is_byhost is a boolean representing whether the preference is to be used as a ByHost.
"""
payload_dict = {}
# Frequency to apply settings, or 'state'
if manage == 'Always':
state = 'Forced'
else:
state = 'Set-Once'
if is_byhost:
domain += '.ByHost'
# Yet another nested dict for the actual contents
payload_dict[domain] = {}
payload_dict[domain][state] = []
payload_dict[domain][state].append({})
payload_dict[domain][state][0]['mcx_preference_settings'] = plist_dict
# Add a datestamp if we're managing 'Once'
if manage == 'Once':
now = NSDate.new()
payload_dict[domain][state][0]['mcx_data_timestamp'] = now
self._addPayload(payload_dict)
def addPayloadFromMCX(self, mcxdata):
"""Add MCX data to the profile's payloads.
"""
# MCX is already 'configured', we just need to add the dict to the payload
self._addPayload(mcxdata)
def finalizeAndSave(self, output_path):
"""Perform last modifications and save to an output plist.
"""
if self.gitrev:
self.data['PayloadDescription'] += "\nGit revision: %s" % self.gitrev[0:10]
writePlist(self.data, output_path)
def makeNewUUID():
return str(uuid4())
def errorAndExit(errmsg):
print >> sys.stderr, errmsg
exit(-1)
def getDomainFromPlist(plist_path_or_name):
"""Assuming the domain is also the name of the plist file, strip the path and the ending '.plist'"""
domain_info = {}
domain_info['is_byhost'] = False
# Match a domain ending in .ByHost, the Ethernet MAC, or the Hardware UUID
byhost_pattern = re.compile('\.ByHost$|\.[0-9a-fA-F]{12}$|\.[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$')
plist_file_name = os.path.basename(plist_path_or_name).split('.plist')[0]
byhost_match = re.search(byhost_pattern, plist_file_name)
if byhost_match:
domain_info['is_byhost'] = True
domain_info['name'] = '.'.join(plist_file_name.split('.')[0:-1])
else:
domain_info['name'] = plist_file_name
return domain_info
def getMCXData(ds_object):
'''Returns a dictionary representation of dsAttrTypeStandard:MCXSettings
from the given DirectoryServices object. This is an array of dicts.'''
ds_object_parts = ds_object.split('/')
ds_node = '/'.join(ds_object_parts[0:3])
ds_object_path = '/' + '/'.join(ds_object_parts[3:])
cmd = ['/usr/bin/dscl', '-plist', ds_node, 'read', ds_object_path,
'dsAttrTypeStandard:MCXSettings']
proc = subprocess.Popen(cmd, bufsize=1, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(pliststr, err) = proc.communicate()
if proc.returncode:
errorAndExit("dscl error: %s" % err)
# decode plist string returned by dscl
try:
mcx_dict = readPlistFromString(pliststr)
except FoundationPlistException:
errorAndExit(
"Could not decode plist data from dscl:\n" % pliststr)
# mcx_settings is a plist encoded inside the plist!
try:
mcx_data_plist = mcx_dict['dsAttrTypeStandard:MCXSettings']
except KeyError:
errorAndExit("No mcx_settings in %s:\n%s" % (ds_object, pliststr))
mcx_data = []
# build a list containing domains' mcx_application_data dict
for mcx_item in mcx_data_plist:
try:
mcx_item = mcx_item.encode('UTF-8')
mcx_item_data = readPlistFromString(str(mcx_item))
mcx_data.append(mcx_item_data['mcx_application_data'])
except KeyError:
errorAndExit(
"Unexpected mcx_settings format in MCXSettings array item:\n%s" % mcx_item)
return mcx_data
def getIdentifierFromProfile(profile_path):
"""Return a tuple containing the PayloadIdentifier and PayloadUUID from the
profile at the path specified."""
profile_dict = readPlist(profile_path)
try:
profile_id = profile_dict['PayloadIdentifier']
profile_uuid = profile_dict['PayloadUUID']
except:
errorAndExit("Can't find a ProfileIdentifier in the profile at %s." % profile_path)
return (profile_id, profile_uuid)
def main():
parser = optparse.OptionParser()
parser.set_usage(
"""usage: %prog [--dsobject DSOBJECT | --plist PLIST]
[--identifier IDENTIFIER | --identifier-from-profile PATH] [options]
One of '--dsobject' or '--plist' must be specified, and only one identifier option.
Run '%prog --help' for more information.""")
# Required options
parser.add_option('--dsobject', '-d', metavar='DSOBJECT',
help="""Directory Services object from which to convert MCX data.
Examples: /Local/Default/Computers/foo
/LDAPv3/some_ldap_server/ComputerGroups/bar""")
parser.add_option('--plist', '-p', action="append", metavar='PLIST_FILE',
help="""Path to a plist to be added as a profile payload.
Can be specified multiple times.""")
parser.add_option('--identifier', '-i',
action="store",
help="""Top-level payload identifier. This is used to uniquely identify a profile.
A profile can be removed using this identifier using the 'profiles' command and the '-R -p' options.""")
parser.add_option('--identifier-from-profile', '-f',
action="store",
metavar="PATH",
help="""Path to an existing .mobileconfig file from which to copy the identifier
and UUID, as opposed to specifying it with the --identifier option.""")
# Optionals
parser.add_option('--removal-allowed', '-r',
action="store_true",
default=False,
help="""Specifies that the profile can be removed.""")
parser.add_option('--organization', '-g',
action="store",
default="",
help="Cosmetic name for the organization deploying the profile.")
parser.add_option('--output', '-o',
action="store",
metavar='PATH',
help="Output path for profile. Defaults to 'identifier.mobileconfig' in the current working directory.")
parser.add_option('--displayname',
action="store",
default="",
help="Display name for profile. Defaults to 'MCXToProfile: <first domain>'.")
# Plist-specific
plist_options = optparse.OptionGroup(parser,
title="Plist-specific options",
description="""These options are useful only in conjunction with --plist.
If multiple plists are supplied, they are applied to all, not on a
per-plist basis.""")
parser.add_option_group(plist_options)
plist_options.add_option('--manage', '-m',
action="store",
help="Management frequency - Once, Often or Always. Defaults to Always.")
options, args = parser.parse_args()
if len(args):
parser.print_usage()
sys.exit(-1)
if options.dsobject and options.plist:
parser.print_usage()
errorAndExit("Error: The '--dsobject' and '--plist' options are mutually exclusive.")
if not options.dsobject and not options.plist:
parser.print_usage()
errorAndExit("Error: One of '--dsobject' or '--plist' must be specified.")
if options.dsobject and options.manage:
print options.manage
parser.print_usage()
errorAndExit("Error: The '--manage' option is used only in conjunction with '--plist'. DS Objects already contain this information.")
if (not options.identifier and not options.identifier_from_profile) or \
(options.identifier and options.identifier_from_profile):
parser.print_usage()
sys.exit(-1)
if options.identifier:
identifier = options.identifier
uuid = False
elif options.identifier_from_profile:
if not os.path.exists(options.identifier_from_profile):
errorAndExit("Error reading a profile at path %s" % options.identifier_from_profile)
identifier, uuid = getIdentifierFromProfile(options.identifier_from_profile)
if options.plist:
if not options.manage:
manage = 'Always'
else:
# ensure capitalization
manage = options.manage[0].upper() + options.manage[1:].lower()
if options.output:
output_file = options.output
else:
output_file = os.path.join(os.getcwd(), identifier + '.mobileconfig')
newPayload = PayloadDict(identifier=identifier,
uuid=uuid,
removal_allowed=options.removal_allowed,
organization=options.organization,
displayname=options.displayname)
if options.plist:
for plist_path in options.plist:
if not os.path.exists(plist_path):
errorAndExit("No plist file exists at %s" % plist_path)
try:
source_data = readPlist(plist_path)
except FoundationPlistException:
errorAndExit("Error decoding plist data in file %s" % plist_path)
source_domain = getDomainFromPlist(plist_path)
newPayload.addPayloadFromPlistContents(source_data,
source_domain['name'],
manage,
is_byhost=source_domain['is_byhost'])
if options.dsobject:
mcx_data = getMCXData(options.dsobject)
# Each domain in the MCX blob gets its own payload
for mcx_domain in mcx_data:
newPayload.addPayloadFromMCX(mcx_domain)
newPayload.finalizeAndSave(output_file)
if __name__ == "__main__":
main()