forked from jacobsalmela/tccutil
/
tccutil.py
executable file
·294 lines (235 loc) · 7.55 KB
/
tccutil.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
#!/usr/bin/env python
import argparse
import sqlite3
import sys
import os
import hashlib
from platform import mac_ver
from distutils.version import StrictVersion as version
##############################
######## VARIABLES ###########
# Utility Name
util_name = os.path.basename(sys.argv[0])
# Utility Version
util_version = '1.2.3'
# Current OS X version
osx_version = version(mac_ver()[0])
# Database Path
tcc_db = '/Library/Application Support/com.apple.TCC/TCC.db'
# Set "sudo" to True if called with Admin-Privileges.
sudo = True if os.getuid() == 0 else False
# Default Verbosity
verbose = False
parser = argparse.ArgumentParser(description='Modify Accesibility Preferences')
parser.add_argument(
'action',
metavar='ACTION',
type=str,
nargs='?',
help='This option is only used to perform a reset.',
)
parser.add_argument(
'--list', '-l', action='store_true',
help="List all entries in the accessibility database."
)
parser.add_argument(
'--insert', '-i', action='append', default=[],
help="Adds the given bundle ID or path to the accessibility database.",
)
parser.add_argument(
"-v", "--verbose", action='store_true',
help="Outputs additional info for some commands.",
)
parser.add_argument(
"-r", "--remove", action='append', default=[],
help="Removes the given Bundle ID or Path from the Accessibility Database.",
)
parser.add_argument(
"-e", "--enable", action='append', default=[],
help="Enables Accessibility Access for the given Bundle ID or Path.",
)
parser.add_argument(
"-d", "--disable", action='append', default=[],
help="Disables Accessibility Access for the given Bundle ID or Path."
)
parser.add_argument(
'--version', action='store_true',
help="Show the version of this script",
)
##############################
######## FUNCTIONS ###########
def display_version():
#------------------------
print "%s %s" % (util_name, util_version)
sys.exit(0)
def sudo_required():
#------------------------
if not sudo:
print "Error:"
print " When accessing the Accessibility Database, %s needs to be run with admin-privileges.\n" % (util_name)
display_help(1)
def open_database():
#------------------------
sudo_required()
global conn
global c
# Check if Datebase is already open, else open it.
try: conn.execute("")
except:
verbose_output("Opening Database...")
try:
if not os.path.isfile(tcc_db):
print "TCC Database has not been found."
sys.exit(1)
conn = sqlite3.connect(tcc_db)
c = conn.cursor()
# Do a sanity check that TCC access table has expected structure
c.execute("SELECT sql FROM sqlite_master WHERE name='access' and type='table'")
accessTableDigest=""
for row in c.fetchall():
accessTableDigest=hashlib.sha1(row[0]).hexdigest()[0:10]
break;
# check if table in DB has expected structure:
if not (
accessTableDigest == "8e93d38f7c" #prior to El Capitan
or
(osx_version >= version('10.11') and accessTableDigest in ["9b2ea61b30", "1072dc0e4b"])
):
print "TCC Database structure is unknown."
sys.exit(1)
verbose_output("Database opened.\n")
except:
print "Error opening Database."
sys.exit(1)
def display_version(error_code=None):
print "%s %s" % (util_name, util_version)
sys.exit(0)
def sudo_required():
if not sudo:
print "Error:"
print " When accessing the Accessibility Database, %s needs to be run with admin-privileges.\n" % (util_name)
display_help(1)
def display_help(error_code=None):
parser.print_help()
if error_code != None: sys.exit(error_code)
#------------------------
print "%s %s" % (util_name, util_version)
sys.exit(0)
def close_database():
try:
conn.execute("")
try:
verbose_output("Closing Database...")
conn.close()
try:
conn.execute("")
except:
verbose_output("Database closed.")
except:
print "Error closing Database."
sys.exit(1)
except:
pass
def commit_changes():
#------------------------
# Apply the changes and close the sqlite connection.
verbose_output("Committing Changes...\n")
conn.commit()
def verbose_output(*args):
#------------------------
if verbose:
try:
for a in args:
print a
except:
pass
def list_clients():
#------------------------
open_database()
c.execute("SELECT client from access")
verbose_output("Fetching Entries from Database...\n")
for row in c.fetchall():
# Print each entry in the Accessibility pane.
print row[0]
verbose_output("")
def cli_util_or_bundle_id(client):
#------------------------
global client_type
# If the app starts with a slash, it is a command line utility.
# Setting the client_type to 1 will make the item visible in the GUI so you can manually click the checkbox.
if (client[0] == '/'):
client_type = 1
verbose_output("Detected \"%s\" as Command Line Utility." % (client))
# Otherwise, the app will be a bundle ID, which starts with a com., net., or org., etc.
else:
client_type = 0
verbose_output("Detected \"%s\" as Bundle ID." % (client))
def insert_client(client):
#------------------------
open_database()
# Check if it is a command line utility or a bundle ID as the default value to enable it is different.
cli_util_or_bundle_id(client)
verbose_output("Inserting \"%s\" into Database..." % (client))
if osx_version >= version('10.11'): # El Capitan or higher.
c.execute("INSERT or REPLACE INTO access VALUES('kTCCServiceAccessibility','%s',%s,1,1,NULL,NULL)" % (client, client_type))
else: # Yosemite or lower.
c.execute("INSERT or REPLACE INTO access VALUES('kTCCServiceAccessibility','%s',%s,1,1,NULL)" % (client, client_type))
commit_changes()
def delete_client(client):
#------------------------
open_database()
verbose_output("Removing \"%s\" from Database..." % (client))
c.execute("DELETE from access where client IS '%s'" % (client))
commit_changes()
def enable(client):
#------------------------
open_database()
verbose_output("Enabling %s..." % (client,))
# Setting typically appears in System Preferences right away (without closing the window).
# Set to 1 to enable the client.
c.execute("UPDATE access SET allowed='1' WHERE client='%s'" % (client))
commit_changes()
def disable(client):
#------------------------
open_database()
verbose_output("Disabling %s..." % (client,))
# Setting typically appears in System Preferences right away (without closing the window).
# Set to 0 to disable the client.
c.execute("UPDATE access SET allowed='0' WHERE client='%s'" % (client))
commit_changes()
def main():
#------------------------
# If no arguments are specified, show help menu and exit.
if not sys.argv[1:]:
print "Error:"
print " No arguments.\n"
display_help(2)
args = parser.parse_args()
if args.version:
display_version()
return
if args.action:
if args.action == 'reset':
exit_status = os.system("tccutil {}".format(' '.join(sys.argv[1:])))
sys.exit(exit_status/256)
else:
print "Error\n Unrecognized command {}".format(args.action)
if args.verbose:
# If verbose option is set, set verbose to True and remove all verbose arguments.
global verbose
verbose = True
if args.list:
list_clients()
return
for item_to_remove in args.remove:
delete_client(item_to_remove)
for item in args.insert:
insert_client(item)
for item in args.enable:
enable(item)
for item in args.disable:
disable(item)
close_database()
sys.exit(0)
if __name__ == '__main__':
main()