/
TCC_Jamf_Extension_Attribute.xml
executable file
·196 lines (163 loc) · 6.98 KB
/
TCC_Jamf_Extension_Attribute.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<extensionAttribute>
<displayName>MunkiReport TCC Module Script</displayName>
<displayInCategory>Extension Attributes</displayInCategory>
<description>This attribute is used by MunkiReport's TCC module: https://github.com/tuxudo/tcc_jamf</description>
<dataType>string</dataType>
<scriptContentsMac>#!/usr/local/munkireport/munkireport-python2
# Written for MunkiReport by tuxudo
# Part of this scrpt are from https://github.com/carlashley/tccprofile
import os
import subprocess
import sys
import plistlib
import glob
import sqlite3
import platform
def get_dbs():
# Get system TCC db
if os.path.isfile('/Library/Application Support/com.apple.TCC/TCC.db'):
user_paths = "/Library/Application Support/com.apple.TCC/TCC.db" + "\n"
else:
user_paths = ""
# Get all users' home folders
cmd = ['dscl', '.', '-readall', '/Users', 'NFSHomeDirectory']
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, unused_error) = proc.communicate()
for user in output.split('\n'):
if 'NFSHomeDirectory' in user and '/var/empty' not in user:
userpath = user.replace("NFSHomeDirectory: ", "")+'/Library/Application Support/com.apple.TCC/TCC.db'
if os.path.isfile(userpath):
user_paths = userpath + "\n" + user_paths
return user_paths[:-1]
def process_dbs(dbs):
output = []
for db in dbs.split('\n'):
tcc = ReadTCC(tcc_db_path=db)
for entry in tcc.read_db():
result = {}
result['dbpath'] = db.replace("Library/Application Support/com.apple.TCC/TCC.db", "")
# Skip iCloud and HomeKit entries
if entry[0] == "kTCCServiceUbiquity" or entry[0] == "kTCCServiceWillow":
continue
result['service'] = entry[0]
result['client'] = entry[1]
result['client_type'] = entry[2]
if entry[3] >= 2:
result['allowed'] = 1
else:
result['allowed'] = entry[3]
result['prompt_count'] = entry[4]
# Process other columns for newer OS versions Mojave (Darwin 18)
if getDarwinVersion() >= 18:
if entry[5] == "UNUSED":
result['indirect_object_identifier'] = ""
else:
result['indirect_object_identifier'] = entry[5]
result['last_modified'] = str(entry[6])
output.append(result)
return output
def getDarwinVersion():
"""Returns the Darwin version."""
# Catalina -> 10.15.7 -> 19.6.0 -> 19
darwin_version_tuple = platform.release().split('.')
return int(darwin_version_tuple[0])
class Sqlite_db():
'''
Wrapper for sqlite3 that includes some budget error/exception handling.
Usage:
Sqlite_db.connect(db)
Tries to connect, if connection doesn't already exist
Sqlite_db.query('SELECT something FROM table', fetch=False)
Makes the query against the database.
fetch=True will return selected items.
Otherwise query is made as supplied
Sqlite_db.commit_change()
Commits changes made to the database.
Sqlite_db.disconnect(db)
Tries to disconnect.
'''
connection = ''
c = ''
def connect(self, db):
try:
self.connection.execute("")
except Exception:
try:
self.connection = sqlite3.connect(db)
self.c = self.connection.cursor()
except Exception:
raise
sys.exit(1)
def disconnect(self, db):
try:
self.connection.execute("")
try:
self.connection.close()
try:
self.connection.execute("")
except Exception:
raise
except Exception:
raise
except Exception:
pass
def query(self, query_string, fetch=False):
try:
self.c.execute(query_string)
if not fetch:
self.c.execute(query_string)
else:
self.c.execute(query_string)
return self.c.fetchall()
except Exception:
raise
class ReadTCC():
def __init__(self, tcc_db_path):
self.tcc_db = tcc_db_path.rstrip('/')
self.tcc_db = os.path.expandvars(self.tcc_db)
self.tcc_db = os.path.expanduser(self.tcc_db)
self.sqlite = Sqlite_db()
def read_db(self):
if self.tcc_db.startswith('/Library') and os.getuid() != 0:
print('You must be root to read {}'.format(self.tcc_db))
sys.exit(1)
else:
self.sqlite.connect(self.tcc_db)
# Get list of database columns
columns = [i[1] for i in self.sqlite.query('PRAGMA table_info(access)', fetch=True)]
# Process other columns for newer OS versions Big Sur
if 'service' in columns and 'client' in columns and 'client_type' in columns and 'auth_value' in columns and 'auth_reason' in columns and 'indirect_object_identifier' in columns and 'last_modified' in columns:
query = self.sqlite.query('SELECT service, client, client_type, auth_value, auth_reason, indirect_object_identifier, last_modified FROM access', fetch=True)
# Process other columns for Mojave and Catalina
elif 'service' in columns and 'client' in columns and 'client_type' in columns and 'allowed' in columns and 'prompt_count' in columns and 'indirect_object_identifier' in columns and 'last_modified' in columns:
query = self.sqlite.query('SELECT service, client, client_type, allowed, prompt_count, indirect_object_identifier, last_modified FROM access', fetch=True)
else:
query = self.sqlite.query('SELECT service, client, client_type, allowed, prompt_count FROM access', fetch=True)
self.sqlite.disconnect(self.tcc_db)
return query
def main():
"""Main"""
# Check Darwin version, must be greater than Darwin 13 (Mac 10.9)
if getDarwinVersion() < 13:
print ('<result>TCC module requires 10.9 or higher<result>')
exit(0)
# Set the encoding
# The "ugly hack" :P
reload(sys)
sys.setdefaultencoding('utf8')
# Get information about the TCC database
dbs = get_dbs()
result = process_dbs(dbs)
# Write TCC info results to cache
cachedir = '/usr/local/munkireport/scripts/cache'
output_plist = os.path.join(cachedir, 'tcc_jamf.plist')
plistlib.writePlist(result, output_plist)
print ('<result>TCC module cache file successfully generated</result>')
#print plistlib.writePlistToString(result)
if __name__ == "__main__":
main()
</scriptContentsMac>
</extensionAttribute>