-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
tccutil.py
executable file
·383 lines (322 loc) · 12.5 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env python
# ****************************************************************************
# tccutil.py, Utility to modify the macOS Accessibility Database (TCC.db)
#
# Copyright (C) 2020, @jacobsalmela
# Copyright (C) 2023, @tnarik
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License, either Version 2 or any later
# version. This program is distributed in the hope that it will be useful,
# but WITTHOUT ANY WARRANTY. See the included LICENSE file for details.
# *****************************************************************************
import argparse
import sqlite3
import sys
import os
import pwd
import hashlib
from platform import mac_ver
from packaging.version import Version as version
# Utility Name
util_name = os.path.basename(sys.argv[0])
# Utility Version
util_version = '1.5.0'
# Current OS X version
osx_version = version(mac_ver()[0]) # mac_ver() returns 10.16 for Big Sur instead 11.+
# Database Path (System by default)
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
# TCC Service
default_service = "kTCCServiceAccessibility"
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, using "/usr/bin/tccutil".\
See `man tccutil` for additional syntax',
)
parser.add_argument(
'--service', '-s',
default=default_service,
help="Set TCC service"
)
parser.add_argument(
'--list', '-l', action='store_true',
help="List all entries in the accessibility database"
)
parser.add_argument(
'--digest', action='store_true',
help="Print the digest hash of 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 a 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(
'--user', '-u',
nargs="?",
const='',
default=None,
help="Modify accessibility database for a given user (defaults to current, if no additional parameter is provided)",
)
parser.add_argument(
'--version', action='store_true',
help="Show the version of this script",
)
def display_version():
"""Print the version of this utility."""
print(f"{util_name} {util_version}")
sys.exit(0)
def sudo_required():
"""Check if user has root priveleges to access the database."""
if not sudo:
print("Error:", file=sys.stderr)
print(f" When accessing the Accessibility Database, {util_name} needs to be run with admin-privileges.\n", file=sys.stderr)
display_help(1)
def digest_check(digest_to_check):
"""Validates that a digest for the table is one that can be used with tccutil."""
# Do a sanity check that TCC access table has expected structure
accessTableDigest = ""
for row in digest_to_check.fetchall():
accessTableDigest = hashlib.sha1(row[0].encode('utf-8')).hexdigest()[0:10]
break
return accessTableDigest
def open_database(digest=False):
"""Open the database for editing values."""
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
accessTableDigest = digest_check(c.execute("SELECT sql FROM sqlite_master WHERE name='access' and type='table'"))
if digest:
print(accessTableDigest)
sys.exit(0)
# check if table in DB has expected structure:
if not (accessTableDigest == "8e93d38f7c" or # prior to El Capitan
# El Capitan , Sierra, High Sierra
(osx_version >= version('10.11') and
accessTableDigest in ["9b2ea61b30", "1072dc0e4b"]) or
# Mojave and Catalina
(osx_version >= version('10.14') and
accessTableDigest in ["ecc443615f", "80a4bb6912"]) or
# Big Sur and later
(osx_version >= version('10.16') and
accessTableDigest in ["3d1c2a0e97", "cef70648de"]) or
# Sonoma
(osx_version >= version('14.0') and
accessTableDigest in ["34abf99d20", "e3a2181c14", "f773496775"])
):
print(f"TCC Database structure is unknown ({accessTableDigest})", file=sys.stderr)
sys.exit(1)
verbose_output("Database opened.\n")
except TypeError:
print("Error opening Database. You probably need to disable SIP for this to work.", file=sys.stderr)
sys.exit(1)
def display_help(error_code=None):
"""Display help an usage."""
parser.print_help()
if error_code is not None:
sys.exit(error_code)
print(f"{util_name} {util_version}")
sys.exit(0)
def close_database():
"""Close the database."""
try:
conn.execute("")
try:
verbose_output("Closing Database...")
conn.close()
try:
conn.execute("")
except:
verbose_output("Database closed.")
except:
print("Error closing Database.", file=sys.stderr)
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):
"""Show verbose output."""
if verbose:
try:
for a in args:
print(a)
except:
pass
def list_clients():
"""List items in the database."""
open_database()
c.execute(f"SELECT client from access WHERE service is '{service}'")
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):
"""Check if the item is a path or a bundle ID."""
# 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.startswith('/'):
client_type = 1
verbose_output(f'Detected "{client}" as Command Line Utility.')
# Otherwise, the app will be a bundle ID, which starts
# with a com., net., or org., etc.
else:
client_type = 0
verbose_output(f'Detected "{client}" as Bundle ID.')
return client_type
def insert_client(client):
"""Insert a client into the database."""
open_database()
# Check if it is a command line utility or a bundle ID
# as the default value to enable it is different.
client_type = cli_util_or_bundle_id(client)
verbose_output(f'Inserting "{client}" into Database...')
# Sonoma
if osx_version >= version('14.0'):
try:
c.execute(f"INSERT or REPLACE INTO access VALUES('{service}','{client}',{client_type},2,4,1,NULL,NULL,0,'UNUSED',NULL,0, NULL, NULL, NULL,'UNUSED', NULL)")
except sqlite3.OperationalError:
print("Attempting to write a readonly database. You probably need to disable SIP.", file=sys.stderr)
# Big Sur and later
elif osx_version >= version('10.16'):
try:
c.execute(f"INSERT or REPLACE INTO access VALUES('{service}','{client}',{client_type},2,4,1,NULL,NULL,0,'UNUSED',NULL,0,0)")
except sqlite3.OperationalError:
print("Attempting to write a readonly database. You probably need to disable SIP.", file=sys.stderr)
# Mojave through Big Sur
elif osx_version >= version('10.14'):
c.execute(f"INSERT or REPLACE INTO access VALUES('{service}','{client}',{client_type},1,1,NULL,NULL,NULL,'UNUSED',NULL,0,0)")
# El Capitan through Mojave
elif osx_version >= version('10.11'):
c.execute(f"INSERT or REPLACE INTO access VALUES('{service}','{client}',{client_type},1,1,NULL,NULL)")
# Yosemite or lower
else:
c.execute(f"INSERT or REPLACE INTO access VALUES('{service}','{client}',{client_type},1,1,NULL)")
commit_changes()
def delete_client(client, service):
"""Remove a client from the database."""
open_database()
verbose_output(f'Removing "{client}" from Database...')
try:
client_type = cli_util_or_bundle_id(client)
if client_type == 1:
c.execute(f"DELETE from access where client='{client}' AND service='{service}'")
else:
c.execute(f"DELETE from access where client LIKE '%{client}%' AND service='{service}'")
except sqlite3.OperationalError:
print("Attempting to write a readonly database. You probably need to disable SIP.", file=sys.stderr)
commit_changes()
def enable(client):
"""Add a client from the database."""
open_database()
verbose_output(f'Enabling {client}...')
# Setting typically appears in System Preferences
# right away (without closing the window).
# Set to 1 to enable the client.
enable_mode_name = 'auth_value' if osx_version >= version('10.16') else 'allowed'
enable_value = '2' if osx_version >= version('12.0') else '1'
try:
c.execute(f"UPDATE access SET {enable_mode_name}='{enable_value}' WHERE client='{client}' AND service IS '{service}'")
except sqlite3.OperationalError:
print("Attempting to write a readonly database. You probably need to disable SIP.", file=sys.stderr)
commit_changes()
def disable(client):
"""Disable a client in the database."""
open_database()
verbose_output(f"Disabling {client}...")
# Setting typically appears in System Preferences
# right away (without closing the window).
# Set to 0 to disable the client.
enable_mode_name = 'auth_value' if osx_version >= version('10.16') else 'allowed'
try:
c.execute(f"UPDATE access SET {enable_mode_name}='0' WHERE client='{client}' AND service IS '{service}'")
except sqlite3.OperationalError:
print("Attempting to write a readonly database. You probably need to disable SIP.", file=sys.stderr)
commit_changes()
def main():
"""Run the main function."""
# 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, rest = parser.parse_known_args()
if args.version:
display_version()
return
global tcc_db
if args.user != None:
try:
if (len(args.user) > 0): pwd.getpwnam(args.user)
tcc_db = os.path.abspath(os.path.expanduser(f'~{args.user}/{tcc_db}'))
except KeyError:
print(f'User "{args.user}" does not exist. Do you mean to use "{args.user}" as ACTION?', file=sys.stderr)
sys.exit(1)
if args.action:
if args.action == 'reset':
exit_status = os.system(f'/usr/bin/tccutil -v {" ".join(rest)}')
sys.exit(exit_status / 256)
else:
print(f'Error\n Unrecognized command "{args.action}"', file=sys.stderr)
global service
service = args.service
if args.verbose:
# If verbose option is set, set verbose to True and remove all verbose arguments.
global verbose
verbose = True
if args.digest:
open_database(digest=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()