forked from gregneagle/pycreateuserpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createuserpkg
executable file
·158 lines (135 loc) · 5.63 KB
/
createuserpkg
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
#!/usr/bin/python
# Copyright 2017 Greg Neagle.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''createuserpkg.py
A tool for creating Apple installer packages that create/update user accounts
on macOS. Much code borrowed and/or inpsired by Per Olofsson's CreateUserPkg'''
import optparse
import sys
import getpass
import os
from locallibs import kcpassword
from locallibs import shadowhash
from locallibs import userplist
from locallibs import userpkg
from locallibs import productbuild
def main():
'''Main'''
usage = "usage: %prog [options] /path/to/output.pkg"
parser = optparse.OptionParser(usage=usage)
required_user_options = optparse.OptionGroup(
parser, 'Required User Options')
required_user_options.add_option(
'--name', '-n', help='User shortname. REQUIRED.')
required_user_options.add_option(
'--uid', '-u', help='User uid. REQUIRED.')
required_package_options = optparse.OptionGroup(
parser, 'Required Package Options')
required_package_options.add_option(
'--version', '-V', help='Package version number. REQUIRED.')
required_package_options.add_option(
'--identifier', '-i', help='Package identifier. REQUIRED.')
optional_user_options = optparse.OptionGroup(
parser, 'Optional User Options')
optional_user_options.add_option(
'--password', '-p',
help='User password. If this is not provided, interactively prompt for '
'password.')
optional_user_options.add_option(
'--fullname', '-f', help='User full name. Optional.')
optional_user_options.add_option('--gid', '-g', help='User gid. Optional.')
optional_user_options.add_option(
'--generateduid', '-G', help='GeneratedUID (UUID). Optional.')
optional_user_options.add_option(
'--hint', help='User password hint. Optional.')
optional_user_options.add_option(
'--home', '-H', help='Path to user home directory. Optional.')
optional_user_options.add_option(
'--shell', '-s', help='User shell path. Optional.')
optional_user_options.add_option(
'--admin', '-a', action='store_true',
help='User account should be added to admin group.')
optional_user_options.add_option(
'--autologin', '-A', action='store_true',
help='User account should automatically login.')
optional_user_options.add_option('--hidden', action='store_true',
help='User account should be hidden.')
optional_package_options = optparse.OptionGroup(
parser, 'Optional Package Options')
optional_package_options.add_option('--distribution', '-d', action='store_true', help='Creates a distribution-style package for use with startosinstall')
parser.add_option_group(required_user_options)
parser.add_option_group(required_package_options)
parser.add_option_group(optional_user_options)
parser.add_option_group(optional_package_options)
options, arguments = parser.parse_args()
# verify options and arguments
required_options = ('name', 'uid', 'version', 'identifier')
missing_required_options = False
for option in required_options:
if not hasattr(options, option) or getattr(options, option) is None:
print >> sys.stderr, 'Missing required option: %s' % option
missing_required_options = True
if missing_required_options:
parser.print_help()
exit(-1)
if len(arguments) != 1:
print >> sys.stderr, "Must provide exactly one filename!"
parser.print_help()
exit(-1)
filename = arguments[0]
if options.password == 'none':
password=""
elif options.password:
password = options.password
else:
password = getpass.getpass('Password: ')
password_again = getpass.getpass('Password (again): ')
if password != password_again:
print >> sys.stderr, "Password mismatch!"
exit(-1)
# make user plist
user_data = {'name': options.name,
'uid': options.uid,
'ShadowHashData': shadowhash.generate(password)}
if options.fullname:
user_data['realname'] = options.fullname
if options.gid:
user_data['gid'] = options.gid
if options.generateduid:
user_data['uuid'] = options.generateduid
if options.home:
user_data['home'] = options.home
if options.shell:
user_data['shell'] = options.shell
if options.hidden:
user_data['IsHidden'] = u'YES'
if options.hint:
user_data['hint'] = options.hint
user_plist = userplist.generate(user_data)
# set up package options/choices
pkg_data = {'version': options.version,
'pkgid': options.identifier,
'destination_path': filename,
'user_plist': user_plist}
if options.autologin:
pkg_data['kcpassword'] = kcpassword.generate(password)
if options.admin:
pkg_data['is_admin'] = True
createuserpkg_dir = os.path.dirname(os.path.realpath(__file__))
# build the package
userpkg.generate(pkg_data, createuserpkg_dir)
if options.distribution:
productbuild.generate(pkg_data, createuserpkg_dir)
if __name__ == '__main__':
main()